What are the practical differences between "associated" and "indexed" arrays in PHP?

The type of the PHP array is actually more like an ordered map than the traditional array C. This is the original PHP shared data structure. The manual states that Indexed and Associative array types are the same type in PHP, which can contain both integer and string indexes .

However, there are many cases where the built-in functions of the language will distinguish between β€œindexed” arrays (arrays with sequential, integer keys) and β€œassociative” arrays (arrays with disjoint and / or keys of mixed types).

One example of this is the array_merge function.

If the input arrays have the same string keys, then a later value for this key will overwrite the previous one. If, however, the arrays contain numeric keys, a later value will not overwrite the original value, but will be added.

If only one array is specified and the array is numerically indexed, the keys will be re-indexed continuously.

What are other places in PHP where the distinction is made between indexed and associative arrays? I'm particularly interested in the differences in Userland, although any understanding of the implementation of Array in a PHP source would be interesting.

+7
arrays php data-structures language-design
source share
5 answers

The most common one that comes to mind is that an indexed array can be looped using the traditional for loop, while an associative array cannot (since it has no number indices):

 for ($i = 0; $i < count($indexed_array); $i++) { // do something with $indexed_array[$i] } 

Of course, php also has the foreach keyword, which works the same for both types.

+3
source share

In fact, any array, whether it is indexed or associative, is a hash table (plus a doubly linked list to maintain element order) in PHP. However, in userland PHP code, indexed and associative arrays almost always have different purposes, and sometimes they need to be handled differently, so several functions, such as sort / asort , make the difference between them just for convenience.

+5
source share

.. and then SplFixedArray , starting with 5.3, it only supports integer indices, has a fixed size and, as a rule, is faster than native arrays.

+3
source share

One interesting difference I found is to use json_encode .

 json_encode(array(0=>0,1=>1,2=>2)); > [0,1,2] json_encode(array(0=>0,2=>2)); > {"0":0,"2":2} 

As a single example, this makes sense, but it is more surprising when combined, for example, with array_filter .

 $f = function($x) { return $x != 1; }; json_encode(array_filter(array(0,1,2), $f)); > {"0":0,"2":2} 

We started with a numerical array, filtered out some elements, but the resulting json is an associative array!


Note that we can get the desired json using array_values .

 json_encode(array_values(array_filter(array(0,1,2),$f))); > [0,2] 
+2
source share

Almost all kernel sorting functions (with all variants of sort , ksort , asort , depending on whether you want to maintain key binding, etc.).

0
source share

All Articles