11

When pushing a new value onto an indexed array

$array[] = 'new value';

the PHP documentation explains how it gets added in the [MAX_INDEX+1] position.

When pushing a new value onto an associative array

$array['key'] = 'new value';

it works the same, but I don't see any explanation in the documentation to confirm how or why it does so. The order seems to be consistent in my implementation, but how do I know for sure that the order will remain the same? Does anyone know how PHP implements this on the back-end?

Evil Elf
  • 2,157
  • 3
  • 22
  • 28
  • 2
    I believe the implementation details are to be found in [zend_hash.c](http://svn.php.net/viewvc/php/php-src/trunk/Zend/zend_hash.c?view=markup). The indexes are kept separate I assume, and `->pInternalPointer` shows the last. – mario Mar 16 '11 at 17:42
  • 1
    They are probably just Linked Hash Maps. The linking provides the order regardless of the hashing. – André Paramés Mar 16 '11 at 17:54
  • Great answers! Just what I was looking for! :-) – John Sonderson Jan 17 '15 at 18:33

4 Answers4

3

All PHP Arrays, numeric and associative, are implemented as a so-called "Ordered Hash-Table". This is a data science term which amounts to: "A reasonable fast key-value store that keeps track of the order in which keys and values were inserted". In other words, PHP arrays have a bit of memory bolted on for the purpose of remembering order. Every time you put something in it, PHP automatically puts the order in there as well.

Interestingly, this happens for numeric keys as well- so if you put the values 1,2,3,4,5 into a PHP array, PHP is still separately keeping track of the order. If this sounds wasteful, that's because it is! It does, however, save brain cycles, that can be used to solve other poeple's problems, real and imagined.

cmc
  • 4,294
  • 2
  • 35
  • 34
3

MAX_INDEX actually has nothing to do with ordering.
you can do

$array[5] = 'new value';
$array[1] = 'new value';
$array[105] = 'new value';
$array[2] = 'new value';

and this array will keep that order as well.

PHP array is an ordered map, so, it's a map that keeps the order.
array elements just keep the order since they were added that's all.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

How are associative arrays implemented in PHP? might give you some insight.

It seems that PHP arrays are essentially hash tables, so the order of the array will stay the same until you reorder it (e.g. by sorting the array).

EDIT: It appears this is getting downvoted, allow me to explicitly include the sources I linked to in the comment below here...

n00dle
  • 5,949
  • 2
  • 35
  • 48
  • 8
    Sorry for the necromancy, but this answer makes no sense. If it were a simple hash table, the order of entries should be based on the hash value of each key, not on the sequence of insertions. In fact, that's how hash tables behave in any other language. And yet PHP associative arrays seem to keep track of the sequence of insertions. So the question is still open for me. How does it do that? Can it be relied upon? – Tobia Jul 02 '14 at 16:58
  • http://stackoverflow.com/questions/2350361/how-is-the-php-array-implemented-on-the-c-level – n00dle Jul 03 '14 at 08:40
  • http://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html – n00dle Jul 03 '14 at 08:41
  • @Tobia, apparently each value stored in the hash is linked to the value stored before it and the value stored after as a linked list. So that explains the order. – Leopoldo Sanczyk Oct 13 '19 at 03:09
0

I prefer to rely on ksort. In my experience, arrays stay consistent until you start removing elements. Better to manually sort them and know they're in the order you want.

Ryre
  • 6,135
  • 5
  • 30
  • 47
  • I remember having trouble with removing elements and sorting the arrays. Probably just poor programming on my part, but led me to my habit of always defining the order of the array if I'm relying on it. – Ryre Mar 16 '11 at 17:57