Reputation: 67994
...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?
Upvotes: 6
Views: 172
Reputation: 4849
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.
Upvotes: 6
Reputation: 15464
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
Upvotes: 4
Reputation: 777
In this case the array become associative. All PHP arrays are some sort of Associative arrays and are implemented as a hash table.
Upvotes: 0
Reputation: 522109
All PHP arrays are associative. PHP doesn't have "lists" as some other languages do. Numerically indexed arrays are merely associative arrays with numeric keys. Numeric keys are somewhat special in that they can be omitted when adding items, causing PHP to auto-index these items. Internally numeric indexes may or may not use a different index type (not sure, look at the source if you want to), but they use the same mechanism, they do not represent a vastly different array type with different performance characteristics.
Upvotes: 1