How does PHP index associative arrays?

Within your user defined array you are assigning the keys manually your array means as

array(1 => 'One',3, 2 => 'Two');//[1] => One [2] => 3 [2] => Two

Here we have two identical index and as per DOCS its mentioned that the last overwrite the first

Syntax “index => values”, separated by commas, define index and values. index may be of type string or integer. When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1.

Note that when two identical index are defined, the last overwrite the first.

In order to filter this case you can simply make some changes as

array(1 => 'One',2 =>'Two',3) // array ([1] => One [2] => Two [3] => 3)
array(3,1 => 'One',2 =>'Two') //array ([0] => 3 [1] => One [2] => Two)
array(1 => 'One',2 => 3 ,3 =>'Two')// array([1] => One [2] => 3 [3] => Two)

DOCS CHECK PARAMETERS

Leave a Comment