Re: Array funkiness
| From: | Philip Olson | Date: | Thu, 11 Oct 2001 19:22:57 +0000 |
| Subject: | Re: Array funkiness | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-70848@lists.php.net to get a copy of this message | ||
Sounds like you want to use array_values() Regarding the question,
consider :
$arr = array('a' => 'apple', 'b' => 'banana',
'cranberry');
$arr[] = 'dank';
Now, what is $arr[1] suppose to be, 'banana' or 'dank' ? In your scenerio
it would be 'banana' when really it's 'dank'. Or, are you suggesting
'banana' to also be a value of $arr[3] ? See how it would get confusing?
regards,
Philip Olson
On Thu, 11 Oct 2001, Boget, Chris 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
>