Lists are treated internally as a map with numeric keys in order.
The first key is always 0; so, an array with n elements will have keys from 0 to n-1.
You can add any key to a given array, even if it previously consisted of numeric entries.
The problem arises when adding numeric keys, and later, you try to append an element to the array.
What do you think will happen?
<?php $names = ['A', 'B', 'C']; $names['z'] = 'Z'; $names = 'S'; $names = 'M'; print_r($names); //w ww .j a va2 s . c o m ?>
When trying to append a value, PHP inserts it after the last numeric key, in this case 8.
<?php $names = ['A', 'B', 'C']; print_r($names); // prints 'B' ?>/*from w w w. j a va2s . c o m*/
Finally, trying to access a key that does not exist in an array will return you a null and throw a notice.
<?php $names = ['A', 'B', 'C']; var_dump($names); // null and a PHP notice ?>//from w w w . j a va2 s . c om