2

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?

Nikhil
  • 485
  • 1
  • 4
  • 16
  • Duplicate, see [this question](http://stackoverflow.com/questions/247467/how-are-associative-arrays-implemented-in-php) – pascal Jan 28 '11 at 05:46

1 Answers1

1

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
)
My Other Me
  • 5,007
  • 6
  • 41
  • 48
  • I need to create an array like this. I am storing values to mysql record IDs to later be processed. However in my case, the mysql record IDs start at like 100,000 and go up - so an array would be `$ar = array([100001] => "Blah", [305278] = "Foo", [412731] = "FooBar")` so later I can just reference the array as `$ar[$recordID]` – rolinger Feb 17 '21 at 21:58