-1

I wonder why I don't get the 3 elements of my array.

$array1 = array( "One" => 1, "Two" => 2, "One" => 1 ); 

When I print it:

echo 'array1:<pre>'; print_r($array1); echo '</pre>'; 

I get this:

array1: Array ( [One] => 1 [Two] => 2 ) 

This is not what I want. I need to show the following:

array1: Array ( [One] => 1 [Two] => 2 [One] => 1 ) 

Any help wil be appreciated. Thanks in advance

3

3 Answers 3

2

Your array is a set of key/value pairs. Think of it as a dictionary:

array( "elephant" => "Big grey animal with tusks", "canary" => "Little Yellow Bird", "elephant" => "Candy that tastes like Skittles" ) 

When you print this one, you will always get the second definition of "elephant", just like in your code. Try changing the second "one" => 1 to "one" => 77.

Because PHP reads from top-down, the last one will always be the final answer, just like in variables.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't have duplicate keys in an array. So by declaring the array['one'] you might replace the old value set before for key array['one'].

Comments

0

Use array of array..I think you are trying to achieve that..

But this looks weird array( ['one'] => array(1,3), ['two']=> 2);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.