views:

142

answers:

4

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.

A: 

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

chaos
+3  A: 

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.

Daniel Vandersluis
+1 for good info, but it should be noted that you can loop over both an indexed array and an associate array using the next() function. In other words, associative arrays still have some internal concept of order.
Alan Storm
True, and along those lines, an indexed array doesn't necessarily have the expected order -- rather, the order the array uses (with `foreach` or `next()`, mind you) depends on the order that it was populated, so if $arr[3] was set before $arr[2], that order will be retained when iterated over.
Daniel Vandersluis
+3  A: 

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.

Ignas R
+1 for the distinction between the implementation and userland code. It's userland stuff I'm interested in.
Alan Storm
+3  A: 

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

Anti Veeranna
+1 for the info, but remember that Standard PHP Library Array objects are not going to be compatible with existing PHP array functions, which can be irritating for old hands.
Alan Storm