...than associative arrays?
Do associative arrays take more memory or something?
$arr = array(1, 1, 1);
$arr[10] = 1;
$arr[] = 1; // <- index is 11; does the array become associative here?
Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion. Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers, beginning at 0. Indexed arrays are used when you identify things by their position. Associative arrays have strings as keys and behave more like two-column tables.
PHP indexed array is an array which is represented by an index number by default. All elements of array are represented by an index number which starts from 0. PHP indexed array can store numbers, strings or any object. PHP indexed array is also known as numeric array.
In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.
In short PHP does not have non-associative arrays.
@Sectus has posted a good answer re the underlying implementation of PHP arrays. It is often beneficial to understand what's going on "under the hood". But regardless of their underlying implementation, PHP arrays are exposed to the PHP developer as an ordered-map of values associated to keys (ie, an associated array). Always.
From PHP:Arrays - Manual
An array in PHP is actually an ordered map. A map is a type that associates values to keys.
and
PHP arrays can contain integer and string keys at the same time as PHP does not distinguish between indexed and associative arrays.
and
The key can either be an integer or a string.
The misconception that arrays are numerically indexed is caused by the feature that integer keys are auto-incremented as a matter of convenience, in the case where no key is explicitly specified.
But note, even when all keys are integers, there's no guarantee in PHP that an item exists at eg $arr[0], which to my knowledge is a given in any other language that does use indexed arrays (that is, assuming the indexed array contains at least one element, and is 0-based).
This is not a trivial differentiation. IMHO programmers who rely on PHP arrays behaving like indexed array without thought for the potential consequences or understanding of the difference* may be setting themselves (or future maintainers) up for strange/unexpected behaviour.
*I've qualified this, as there are obviously cases where an informed decision to take advantage of the index-like conveniences/features of the PHP language around arrays can provide benefit.
Array in PHP always an Hash Table. You can read this article by @NikiC.
Basically, everything in PHP is a hash table. Not only are hash tables used in the underlying implementation of PHP arrays, they are also used to store object properties and methods, functions, variables and pretty much everything else.
If keys come in order one by one from 0 it looks like indexed array, but it's not actually true.
Also this article will be useful.
PHP arrays are arrays, dictionaries and linked lists at the same time, that sure needs much info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With