How does PHP handle integer indexed non-consecutive keyed arrays?

798 views Asked by At

I'm curious as to how efficient it is in PHP to store an array with integer indices that are non-consecutive.

If I had an array

$b = array();
$b[1] = "Hello";
$b[18] = "World";
$b[999] = "Test";

Are those keys stored, and then hashed to a new array, how does PHP handle this?

1

There are 1 answers

1
My Other Me On BEST ANSWER

From php wep site on array:

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

Executing print_r($b); on your code gives the following output:

Array
(
    [1] => Hello
    [18] => World
    [999] => Test
)