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?

1
  • Duplicate, see this question Commented Jan 28, 2011 at 5:46

1 Answer 1

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
)
Sign up to request clarification or add additional context in comments.

1 Comment

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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.