简体   繁体   中英

How does PHP index associative arrays?

As I know, in associative arrays , if the keys is not set, it will be set automatically. But it seem doesn't make sense in this case:

$a = array( '1' => 'One', '3', '2' => 'Two');
print_r($a);

Outputs:

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

So where is the '3'?

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

In php The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer key

Like if :-

$a = array( 1 => 'One', 3, 3 => 'Two');
var_dump($a);

output will :-

array(3) {
  [1]=>
  string(3) "One"
  [2]=>
  int(3)
  [3]=>
  string(3) "Two"
}

Here for second value one is increment from previous value ie 2.

Now

say array is :-

$a = array( '1' => 'One', '3', '3' => 'Two');
var_dump($a);

Output will

array(3) {
  [1]=>
  string(3) "One"
  [2]=>
  string(1) "3"
  [3]=>
  string(3) "Two"
}

Here also Here for second value one is increment from previous value ie 2.

Now third case:-

If array is :-

$a = array( '1' => 'One', '1' => 'two' , '1' => 'Three');
var_dump($a);

Output will:-

array(1) {
  [1]=>
  string(5) "Three"
}

This is because associative array keep value as map and if key is present it overwrite value in this case 1 is overwrite 2 time as a result out is three

In your case :-

$a = array( '1' => 'One', '3', '2' => 'Two');
print_r($a);

Output is

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

this is because :-

first key map will:- '1' => 'one'

again

php will keep second value as '2' => '3'

Now as in array '2' is assigned as 'Two', value become

'2' => 'Two' which means it is overwriting.

@Uchiha is right, just as an include to that answer, if you want to avoid this problem, keep members of an array (which are not having keys specified) at the last


$a = array( '1' => 'One', '3', '2' => 'Two');

will dump

array (size=2)
  1 => string 'One' (length=3)
  2 => string 'Two' (length=3)


and keeping members with undefined keys at last

array (size=3)
  1 => string 'One' (length=3)
  2 => string 'Two' (length=3)
  3 => string '3' (length=1)

will dump

 array (size=3) 1 => string 'One' (length=3) 2 => string 'Two' (length=3) 3 => string '3' (length=1) 


will avoid issues you are facing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM