views:

41

answers:

2

Hello,

I have a question in regard to PHP arrays.

If I create an array

$ids = array();

then put something in position 1000 of it:

$ids[1000] = 5;

How would the interpreter do this internally? Are arrays contiguous lumps of memory like Java? Would it be like int[1000] where 1000 ints are initlialized?

Or is it more of a map where 1000 is the key and it links to the data of 5? I am talking about the internals of PHP.

If I have a large number index, would it be less efficient because it would have to initialize all the indexes? OR is it map based with the number as a key?

+2  A: 

All arrays in PHP are effectively associative arrays that maintain insertion order. So:

$array[1000] = 5;

is fundamentally no different to:

$array['hello'] = 'world';
cletus
except that the second example won't work because of a typo, yes. :-)
p4bl0
@p4bl0: I corrected it.
Asaph
+1  A: 

Arrays are hashes in PHP. So in your terms, they are mapped. You can check this in your example by looking at $ids[0...999] after initialization to see that they don't exist and by comparing memory_get_usage() before and after initialization.

fsb