PHP 如何跟踪关联数组中的顺序?
当将新值推送到索引数组时,
$array[] = 'new value';
PHP 文档解释了如何将其添加到 [MAX_INDEX+1] 位置。
当将新值推入关联数组时,
$array['key'] = 'new value';
它的工作原理是相同的,但我在文档中没有看到任何解释来确认它是如何或为什么这样做的。该顺序在我的实现中似乎是一致的,但我如何确定该顺序将保持不变?有谁知道PHP后端是如何实现的?
When pushing a new value onto an indexed array
$array[] = 'new value';
the PHP documentation explains how it gets added in the [MAX_INDEX+1] position.
When pushing a new value onto an associative array
$array['key'] = 'new value';
it works the same, but I don't see any explanation in the documentation to confirm how or why it does so. The order seems to be consistent in my implementation, but how do I know for sure that the order will remain the same? Does anyone know how PHP implements this on the back-end?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所有 PHP 数组,无论是数值数组还是关联数组,都被实现为所谓的“有序哈希表”。这是一个数据科学术语,相当于:“一个合理的快速键值存储,可以跟踪键和值的插入顺序”。换句话说,PHP 数组有一些内存用于记住顺序。每次你往里面放东西时,PHP 也会自动把顺序放在那里。
有趣的是,数字键也会发生这种情况 - 因此,如果您将值 1,2,3,4,5 放入 PHP 数组中,PHP 仍然会单独跟踪顺序。如果这听起来很浪费,那是因为它确实很浪费!然而,它确实节省了大脑周期,可以用来解决其他人的问题,无论是真实的还是想象的。
All PHP Arrays, numeric and associative, are implemented as a so-called "Ordered Hash-Table". This is a data science term which amounts to: "A reasonable fast key-value store that keeps track of the order in which keys and values were inserted". In other words, PHP arrays have a bit of memory bolted on for the purpose of remembering order. Every time you put something in it, PHP automatically puts the order in there as well.
Interestingly, this happens for numeric keys as well- so if you put the values 1,2,3,4,5 into a PHP array, PHP is still separately keeping track of the order. If this sounds wasteful, that's because it is! It does, however, save brain cycles, that can be used to solve other poeple's problems, real and imagined.
MAX_INDEX 实际上与排序无关。
你可以这样做
,这个数组也将保持这个顺序。
PHP 数组是一个有序映射,因此,它是一个保持顺序的映射。
数组元素只保持添加后的顺序
就这样。
MAX_INDEX actually has nothing to do with ordering.
you can do
and this array will keep that order as well.
PHP array is an ordered map, so, it's a map that keeps the order.
array elements just keep the order since they were added
that's all.
关联数组是如何在 PHP 中实现的? 可能会给您一些见解。
看来 PHP 数组本质上是哈希表,因此数组的顺序将保持不变,直到您重新排序(例如通过对数组进行排序)。
编辑:看来这被否决了,请允许我在下面的评论中明确包含我链接到的来源...
“PHP 关联数组实际上是 HashTables 的实现”,从
PHP 数组在 C 级别是如何实现的?< /a>
同样来自该来源:“PHP 数组是一个链式哈希表(在键冲突时查找 O(c) 和 O(n)),允许使用 int 和 string 键。它使用 2 种不同的哈希算法将两种类型放入相同的哈希键空间中。”
“一切都是哈希表”来自 http://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html
How are associative arrays implemented in PHP? might give you some insight.
It seems that PHP arrays are essentially hash tables, so the order of the array will stay the same until you reorder it (e.g. by sorting the array).
EDIT: It appears this is getting downvoted, allow me to explicitly include the sources I linked to in the comment below here...
"PHP associative arrays are in fact an implementation of HashTables", from
How is the PHP array implemented on the C level?
Also from that source: "The PHP array is a chained hash table (lookup of O(c) and O(n) on key collisions) that allows for int and string keys. It uses 2 different hashing algorithms to fit the two types into the same hash key space."
"Everything is a HashTable" from http://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html
我更喜欢依赖 ksort。根据我的经验,数组会保持一致,直到您开始删除元素。最好手动对它们进行排序并知道它们按照您想要的顺序排列。
I prefer to rely on ksort. In my experience, arrays stay consistent until you start removing elements. Better to manually sort them and know they're in the order you want.