[SOLVED]simple array question
Moderator: General Moderators
-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am
[SOLVED]simple array question
sorry if this is an obvious one but i've been working all night and my brain's fried and i can' sort this out.
i have a variable that has concatenated variables, 10 in this case. why, when i explode this into an array, does a count of the array return 11? even if i print_r there are only 10 values printed.
i'm sure i've come across this before but i cant remember how i fixed it?
any ideas??
rj
i have a variable that has concatenated variables, 10 in this case. why, when i explode this into an array, does a count of the array return 11? even if i print_r there are only 10 values printed.
i'm sure i've come across this before but i cant remember how i fixed it?
any ideas??
rj
Last edited by rubberjohn on Fri Mar 10, 2006 12:54 pm, edited 1 time in total.
-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am
thanks for replying hawleyjr
if i echo $var_saved_tags i get 10 values separated by a '.' but when i explode to an array $array_saved_tags it has a count of 11 but print(_r)'s 10 values.
im not even sure if this is the right way to return an array of values into an array!! is there a better way of doing this?
thanks rj
Code: Select all
while($get_tag_name = mysql_fetch_array($sql_get_tag_name, MYSQL_ASSOC)){
$var_saved_tags .= $get_tag_name[tag] . ".";
}
$array_saved_tags = explode("." ,$var_saved_tags);im not even sure if this is the right way to return an array of values into an array!! is there a better way of doing this?
thanks rj
this is because your concatenated string will end up like this...
a.b.c.
when you explode you will get
a
b
c
[empty string]
if you add a substring($var_saved_tags,0,-1); at the end of you loop it should work fine.
edit:
a better way of doing this is inside your loop put
$array_saved_tags[] = $get_tag_name[tag]
a.b.c.
when you explode you will get
a
b
c
[empty string]
if you add a substring($var_saved_tags,0,-1); at the end of you loop it should work fine.
edit:
a better way of doing this is inside your loop put
$array_saved_tags[] = $get_tag_name[tag]
- R4000
- Forum Contributor
- Posts: 168
- Joined: Wed Mar 08, 2006 12:50 pm
- Location: Cambridge, United Kingdom
You should use this little function i created:
This will clean all the empty things out of your array, making it work 
Code: Select all
function arrayClean($array) {
foreach ($array as $index => $value) {
if (empty($value)) unset($array[$index]);
}
return $array;
}-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am
Why are you not storing the data in an array to begin with?
To:
Code: Select all
while($get_tag_name = mysql_fetch_array($sql_get_tag_name, MYSQL_ASSOC)){
$var_saved_tags .= $get_tag_name[tag] . ".";
}
$array_saved_tags = explode("." ,$var_saved_tags);Code: Select all
$var_saved_tags = array();
while($get_tag_name = mysql_fetch_array($sql_get_tag_name, MYSQL_ASSOC)){
$var_saved_tags[] = $get_tag_name[tag];
}-
rubberjohn
- Forum Contributor
- Posts: 193
- Joined: Fri Feb 25, 2005 4:03 am