Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does PHP keep track of order in an associative array?

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?

like image 657
Evil Elf Avatar asked Mar 16 '11 17:03

Evil Elf


People also ask

Are PHP associative arrays ordered?

So yes, they are always ordered. Arrays are implemented as a hash table.

How do you sort an associative array in PHP?

The arsort() function sorts an associative array in descending order, according to the value. Tip: Use the asort() function to sort an associative array in ascending order, according to the value. Tip: Use the krsort() function to sort an associative array in descending order, according to the key.

What are the advantages of associative arrays in PHP?

Advantages of Associative Array We can save more data, as we can have a string as key to the array element, where we can have associated data to the value to be stored, like in our example, we stored the type of the car as key along with the name of the car as value.

What is associative arrays in PHP explain it with simple PHP code?

Associative Array - It refers to an array with strings as an index. Rather than storing element values in a strict linear index order, this stores them in combination with key values. Multiple indices are used to access values in a multidimensional array, which contains one or more arrays.


3 Answers

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.

like image 198
Your Common Sense Avatar answered Oct 05 '22 11:10

Your Common Sense


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.

like image 33
cmc Avatar answered Oct 05 '22 11:10

cmc


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...

  • "PHP associative arrays are in fact an implementation of HashTables", from How is the PHP array implemented on the C level?

  • Also from that source: "The PHP array is a chained hash table (lookup of O(c) and O(n) on key collisions) that allows for int and string keys. It uses 2 different hashing algorithms to fit the two types into the same hash key space."

  • "Everything is a HashTable" from http://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html

like image 28
n00dle Avatar answered Oct 05 '22 12:10

n00dle