RE: [PHP] weird array behaviour

From: Date: Wed, 15 Aug 2001 15:58:32 +0000
Subject: RE: [PHP] weird array behaviour
Groups: php.general 
Request: Send a blank email to php-general+get-62855@lists.php.net to get a copy of this message
Nick, I also have noticed "weird" behavior with multi (or 2D) arrays. Read more on the manual board about arrays. However, I have finally figured out something that works. Be careful of you you define the subarray, assign values, and read the array. Here's what works for me: Creating: (you could also use array_push(), but I don't think that is supported by PHP3) $myarray = array(); $myarray[] = array(); Assigning: $myarray[$i][$j] = "some value"; (j can be blank or i and j can be hardcoded integers). Reading (this was the most tricky for me): returning $myarray[$i][$j] didn't seem to work (although the manual says it should). It seems to work when $i and $j are hardcoded integers, ie $myarray[0][1] When $i and $j must be variables like your $key and $value, I finally went with nested for loops (this works): function print_array2D($array) { if(gettype($array)=="array") { $maxi = count($array) - 1; reset($array); for ($i=0; $i<=$maxi; $i++) { echo "<li><b>$i:</b> "; $subarray = $array[$i]; for ($j=0; $j<2; $j++) { echo "$subarray[$j] "; } echo "</li>"; } } else { echo $array; } } Johan -----Original Message----- From: Nick Davies [mailto:nick@sensei.co.uk] Sent: Wednesday, August 15, 2001 9:33 AM To: php-general@lists.php.net Subject: [PHP] weird array behaviour I have a function which returns a mulit-dimensional array, but the array which is returned doesn't "work" for some reason :/ If i print the array within the function using while (list($key) = each($myArray)) { while (list($key2, $value2) = each($myArray[$key])) { print $key . " : " . $key . " : " . $value; } } it works fine but doing the same thing after the array has been returned jus yields -- Warning: Variable passed to each() is not an array or object in ... refering to $myArray[$key]; even though if i print $myArray[$key] it tells me that it's an Array :/ Thanks. Nick. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#62855) next »