PHP 如何跟踪关联数组中的顺序?

发布于 2024-10-23 12:27:05 字数 287 浏览 7 评论 0原文

当将新值推送到索引数组时,

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑饮青盏花 2024-10-30 12:27:06

所有 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.

蓝颜夕 2024-10-30 12:27:06

MAX_INDEX 实际上与排序无关。
你可以这样做

$array[5] = 'new value';
$array[1] = 'new value';
$array[105] = 'new value';
$array[2] = 'new value';

,这个数组也将保持这个顺序。

PHP 数组是一个有序映射,因此,它是一个保持顺序的映射。
数组元素只保持添加后的顺序
就这样。

MAX_INDEX actually has nothing to do with ordering.
you can do

$array[5] = 'new value';
$array[1] = 'new value';
$array[105] = 'new value';
$array[2] = 'new value';

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.

昵称有卵用 2024-10-30 12:27:06

关联数组是如何在 PHP 中实现的? 可能会给您一些见解。

看来 PHP 数组本质上是哈希表,因此数组的顺序将保持不变,直到您重新排序(例如通过对数组进行排序)。

编辑:看来这被否决了,请允许我在下面的评论中明确包含我链接到的来源...

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...

ヅ她的身影、若隐若现 2024-10-30 12:27:06

我更喜欢依赖 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文