关联数组是如何在PHP中实现的?

时间:2022-12-14 12:15:08

Can someone explain how PHP implements associative arrays? What underlying data structure does PHP use? Does PHP hash the key and store it in some kind of hash map? I am curious because I was wondering what the performance of associative arrays where when inserting and searching for keys.

有人能解释一下PHP如何实现关联数组吗?PHP使用什么底层数据结构?PHP哈希键并将其存储在某种哈希映射中吗?我很好奇,因为我想知道在插入和搜索键时关联数组的性能。

5 个解决方案

#1


7  

It's a hash table. The type declaration and hashing function are here:
http://svn.php.net/viewvc/php/php-src/trunk/Zend/zend_hash.h?view=markup

这是一个哈希表。类型声明和散列函数如下:http://svn.php.net/viewvc/php/php- src/trunk/zend/zend_hash.h?

There is a light weight array and a linked list within the spl (standard php lib)

spl中有一个轻量级数组和一个链表(标准php库)

#2


6  

Well, for what it is worth, all PHP arrays are Associative arrays.

所有的PHP数组都是关联数组。

#3


4  

The highest voted answer link is broken and doesn't give that much explanation.

最高投票的答案链接被打破,并没有给出太多的解释。

PHP is written in C and the underlying structure is just a C array. C arrays are just chunks of memory. The indexes in C arrays must be continuous, you can't have an index 0 and an index 1000 that comes after it. To make associative array keys work, before they are added to the C array, they are converted to proper C indices via a hash function.

PHP是用C编写的,底层结构只是一个C数组。C数组只是内存块。C数组中的索引必须是连续的,不能有索引0和索引在它之后。为了使关联数组键起作用,在将它们添加到C数组之前,通过哈希函数将它们转换为适当的C索引。

For a full explanation, I found this link to be much more informative.

为了得到充分的解释,我发现这个链接的信息量更大。

http://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html

http://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html

#4


3  

@EBGreen is correct.

@EBGreen是正确的。

Which gives you some interesting performance problems, especially when treating an array as a list and using the [] (array add) operator. PHP doesn't seem to cache the largest numeric key and add one to it, instead it seems to traverse all of the keys to find what the next numeric key should be. I've rewritten scripts in python because of PHP's dismal array-as-a-list performance.

这给您带来了一些有趣的性能问题,特别是当将数组视为列表并使用[](array add)操作符时。PHP似乎没有缓存最大的数字键并向其添加一个,相反,它似乎遍历了所有的键,以找到下一个数字键应该是什么。由于PHP糟糕的array-as-a-list性能,我用python重写了脚本。

Associative arrays have the standard dict/hash performance overhead.

关联数组具有标准的dict/哈希性能开销。

#5


2  

It's all hash tables, according to sources in various web forums: http://www.usenet-forums.com/php-language/15348-zend-engine-array-implementation.html

根据各种web论坛的消息来源,它都是散列表:http://www.usenet-forums.com/php-language/15348-zend-engine- array-implement.html

If you want to be sure, read the source, then compile it, but make sure you can trust your compiler (Warning: PDF, and unrelated, but very cool).

如果您想要确定,请阅读源代码,然后编译它,但是请确保您可以信任您的编译器(警告:PDF,不相关,但非常酷)。

#1


7  

It's a hash table. The type declaration and hashing function are here:
http://svn.php.net/viewvc/php/php-src/trunk/Zend/zend_hash.h?view=markup

这是一个哈希表。类型声明和散列函数如下:http://svn.php.net/viewvc/php/php- src/trunk/zend/zend_hash.h?

There is a light weight array and a linked list within the spl (standard php lib)

spl中有一个轻量级数组和一个链表(标准php库)

#2


6  

Well, for what it is worth, all PHP arrays are Associative arrays.

所有的PHP数组都是关联数组。

#3


4  

The highest voted answer link is broken and doesn't give that much explanation.

最高投票的答案链接被打破,并没有给出太多的解释。

PHP is written in C and the underlying structure is just a C array. C arrays are just chunks of memory. The indexes in C arrays must be continuous, you can't have an index 0 and an index 1000 that comes after it. To make associative array keys work, before they are added to the C array, they are converted to proper C indices via a hash function.

PHP是用C编写的,底层结构只是一个C数组。C数组只是内存块。C数组中的索引必须是连续的,不能有索引0和索引在它之后。为了使关联数组键起作用,在将它们添加到C数组之前,通过哈希函数将它们转换为适当的C索引。

For a full explanation, I found this link to be much more informative.

为了得到充分的解释,我发现这个链接的信息量更大。

http://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html

http://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html

#4


3  

@EBGreen is correct.

@EBGreen是正确的。

Which gives you some interesting performance problems, especially when treating an array as a list and using the [] (array add) operator. PHP doesn't seem to cache the largest numeric key and add one to it, instead it seems to traverse all of the keys to find what the next numeric key should be. I've rewritten scripts in python because of PHP's dismal array-as-a-list performance.

这给您带来了一些有趣的性能问题,特别是当将数组视为列表并使用[](array add)操作符时。PHP似乎没有缓存最大的数字键并向其添加一个,相反,它似乎遍历了所有的键,以找到下一个数字键应该是什么。由于PHP糟糕的array-as-a-list性能,我用python重写了脚本。

Associative arrays have the standard dict/hash performance overhead.

关联数组具有标准的dict/哈希性能开销。

#5


2  

It's all hash tables, according to sources in various web forums: http://www.usenet-forums.com/php-language/15348-zend-engine-array-implementation.html

根据各种web论坛的消息来源,它都是散列表:http://www.usenet-forums.com/php-language/15348-zend-engine- array-implement.html

If you want to be sure, read the source, then compile it, but make sure you can trust your compiler (Warning: PDF, and unrelated, but very cool).

如果您想要确定,请阅读源代码,然后编译它,但是请确保您可以信任您的编译器(警告:PDF,不相关,但非常酷)。