Re: Array funkiness

From: Date: Thu, 11 Oct 2001 19:49:50 +0000
Subject: Re: Array funkiness
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-70850@lists.php.net to get a copy of this message
not your answer but... print_r($arrayVar); is very handy when figuring out how arrays work. Chris Boget wrote:
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


« previous php.general (#70850) next »