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:[EMAIL PROTECTED]]
Sent: Wednesday, August 15, 2001 9:33 AM
To: [EMAIL PROTECTED]
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]