Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 765
Alan Storm Avatar asked Aug 25 '09 18:08

Alan Storm


People also ask

What is index and associative array in PHP?

An array is a type of variable that may contain several values at once. There are three types of arrays, namely: Indexed array - An array with a numeric key. Associative array — An array where each key has its own specific value. Multidimensional array — An array containing one or more arrays within itself.

What is an associative array in PHP?

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.

What are the two types of PHP arrays and how do they differ?

In PHP there is two kinds of arrays : indexed array and associative array. The only difference is that numeric values are used as 'keys' in indexed array start from zero (0) and in associative array, strings are used as 'keys'.


Video Answer


2 Answers

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.

like image 73
Ignas R Avatar answered Oct 04 '22 20:10

Ignas R


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.

like image 29
Daniel Vandersluis Avatar answered Oct 04 '22 20:10

Daniel Vandersluis