4 ms·
A PHP array is in fact just an ordered hash-map (IE.. hash map with an associated linked list which tracks the iteration order). When you do an array append, t
by birken 15y ago
A PHP array is in fact just an ordered hash-map (IE.. hash map with an associated linked list which tracks the iteration order).
When you do an array append, the logic PHP runs is it tries to guess what the most logical key would be, add that to the hash-map and then to the end of the linked list.
$a = array();
$a[] = 'A';
$a[] = 'B';
print_r($a);
Array
(
[0] => A
[1] => B
)
$b = array();
$b[5] = 'A';
$b[100] = 'B';
$b[] = 'C';
print_r($b);
Array
(
[5] => A
[100] => B
[101] => C
)
- rorrr 15y agoI think they are a little more complex than regular hash maps. 1) They keys can be integers or unicode strings (ordering works differently for these two cases) 2) The keys are not necessarily ordered (see ksort and krsort functions, for example).
- wlievens 15y agoThey are ordered by insertion. That's different from for instance objects in Javascript.
- rorrr 15y agoNot really $arr = array(); $arr[0] = 'cat'; $arr[2] = 'dog'; $arr[1] = 'fish'; krsort($arr); var_dump($arr); Outputs: array(3) { [2]=> string(3) "dog" [1]=> string(4) "fish" [0]=> string(3) "cat" }
- josegonzalez 15y agoWell you are explicitly sorting the array by keys, so its not like you are contradicting his sentence. Remove the krsort() call and your "not really" becomes "example".