Home » Php » What are the Practical Differences Between "associate" and "indexed" Arrays in PHP?

What are the Practical Differences Between "associate" and "indexed" Arrays in PHP?

Posted by: admin July 12, 2020 Leave a comment

Questions:

The PHP array type is actually more akin to an an ordered map than a traditional C array. It’s PHP’s original general use data structure. The manual goes as far to say The indexed and associative array types are the same type in PHP, which can both contain integer and string indices.

However, there’s a lot of cases where built-in language features will make a distinction between "indexed" arrays (arrays with sequential, integer keys) and "associative" arrays (arrays with non-sequential 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 the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way.

What are the other places in PHP where a distinction is made between indexed and associative arrays? I’m specifically interested in the Userland differences, although any insight into the Array implementation in PHP’s source would be interesting as well.

How to&Answers:

The most prevalent one that comes to mind is that an indexed array can be looped over using a traditional for loop, whereas an associative one cannot (because it does not have the numeric indexes):

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

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

Answer:

Actually, any array, no matter if it’s indexed or associative, is a hashtable (plus a doubly-linked list for maintaining the order of elements) in PHP. However, in userland PHP code, indexed and associative arrays almost always serve different purposes and sometimes need to be treated in different ways, so several functions like sort/asort make a distinction between them just for convenience.

Answer:

.. and then there is SplFixedArray, starting from 5.3, it supports only integer indexes, has fixed size and is generally faster than the native arrays.

Answer:

One interesting difference I’ve found is when using 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 lone example this makes sense, but it’s more surprising when combined with, say, array_filter.

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

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


Note that we can get the desired json by using array_values.

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

Answer:

Pretty much all of the core sorting functions (with all the sort, ksort, asort variations depending on whether you want to maintain key association and so on).