开发者

Are numerically indexed PHP arrays initialized contiguously or mapped?

开发者 https://www.devze.com 2022-12-14 18:16 出处:网络
I have a question in regard to PHP arrays. If I create an array $ids = array(); then put something in position 1000 of it:

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 in开发者_JAVA百科ternally? 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?


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

$array[1000] = 5;

is fundamentally no different to:

$array['hello'] = 'world';


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消