Array funkiness
| From: | Boget, Chris | Date: | Thu, 11 Oct 2001 19:00:23 +0000 |
| Subject: | Array funkiness | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-70843@lists.php.net to get a copy of this message | ||
Consider the following:
$myArray = array( "one", "two", "three" );
for( $i = 0; $i < 3; $i++ ) {
echo $myArray[$i] . "<br>\n";
}
The above is fine.
$myArray = array( "one" => "1", "two" => "2",
"three" => "3" );
for( $i = 0; $i < 3; $i++ ) {
echo $myArray[$i] . "<br>\n";
}
why isn't the above working? Why can I not reference by element
number? Why do I have to reference by element name?
while( list( $key, $value ) = each( $myArray )) {
echo "$key => $value<br>\n";
}
Now this... I seem to recall a time when the above would have
echo'd out:
0 => one
1 => two
2 => three
one => 1
two => 2
three => 3
or something along those lines. But it's not. Why?
I guess this all comes down to "Why can you not reference an
array by element numbers when it comes to associative arrays"?
This yields output:
echo "Named: " .
$testArray["jt8ehty"]["September2001"]["declared"] .
"<br>\n";
while this does not:
echo "Numbered: " . $testArray[0][0][1] . "<br>\n";
even though those are the correct elements...
What am I doing wrong?
Chris