Given an associative array like the following,
给定如下的关联数组,
$field_defaults = array(
'id' => 0,
'name' => 'new item',
'desc' => '',
'parent_id' => 0,
);
can I rely on array_keys()
returning the keys in the order they were specified? Or, more precisely, since arrays in PHP seem to have a stable order, as per this answer, are the keys returned by array_keys()
in the same order as they appear in the input array? The manual page doesn't give any hints.
我可以依赖array_keys()按照指定的顺序返回键吗?或者更确切地说,由于PHP中的数组似乎具有稳定的顺序,因此根据这个答案,array_keys()返回的键的顺序与它们在输入数组中出现的顺序相同?手册页没有提供任何提示。
When I try this, they seem to respect the original order, but I would like to be able to rely on that behaviour.
当我尝试这个时,他们似乎尊重原始秩序,但我希望能够依赖这种行为。
2 个解决方案
#1
31
TL;DR: Theoretically you can't count on it; for practical purposes IMO you can.
TL; DR:从理论上讲,你不能指望它;出于实用目的IMO你可以。
Since the docs do not guarantee the ordering then technically the correct answer would be "no, you can't count on that".
由于文档不保证订购,从技术上讲,正确的答案是“不,你不能指望”。
That's because theoretically the developers could have chosen to reserve themselves the option of changing the implementation at a future date so that it does not honor the existing order any more (perhaps to improve performance, or to gain some other benefit).
这是因为从理论上讲,开发人员可以选择保留自己在未来更改实施的选项,这样它就不再支持现有订单(可能是为了提高性能,或者为了获得其他好处)。
Now as a practical matter, we know that the current implementation honors the ordering -- PHP arrays are ordered containers (there is a linked list of values among other things) -- and this is something you wouldn't ever expect to change.
现在作为一个实际问题,我们知道当前的实现遵循排序 - PHP数组是有序容器(除了其他东西之外还有一个值的链接列表) - 这是你不会期望改变的。
If it did, the change would hint to a corresponding significant change in the internal implementation of arrays and that would in turn be likely to break lots of other code too. I don't see it happening any time soon.
如果确实如此,则更改将暗示数组的内部实现中的相应重大更改,这反过来可能会破坏许多其他代码。我不认为它会很快发生。
#2
2
If you are concerned, you could always pick one as the correct order and then reimplement the other function based on that. And if you care about consistency between the two calls, you probably are going to call both array_keys
and array_values
at the same time. So why not do both simultaneously? E.g., let's assume that the order of array_keys()
is "correct". Then do:
如果您担心,您可以随时选择一个作为正确的顺序,然后根据它重新实现其他功能。如果你关心两个调用之间的一致性,你可能会同时调用array_keys和array_values。那么为什么不同时做两个呢?例如,假设array_keys()的顺序是“正确的”。然后做:
function arrayKV($arr) {
$keys = array_keys($arr);
$values = array();
foreach($keys as $key) {
$newArr[] = $arr[$key];
}
return array('keys' => $keys, 'values' => $values);
}
That way, you know that they are in the same order. Alternatively, you could provide the keys to use as the order:
这样,你知道它们的顺序是一样的。或者,您可以提供用作订单的密钥:
function arrayValuesStable($arr, $keys) {
$values = array();
foreach($keys as $key) {
$values[] = $arr[$key];
}
return $values;
}
#1
31
TL;DR: Theoretically you can't count on it; for practical purposes IMO you can.
TL; DR:从理论上讲,你不能指望它;出于实用目的IMO你可以。
Since the docs do not guarantee the ordering then technically the correct answer would be "no, you can't count on that".
由于文档不保证订购,从技术上讲,正确的答案是“不,你不能指望”。
That's because theoretically the developers could have chosen to reserve themselves the option of changing the implementation at a future date so that it does not honor the existing order any more (perhaps to improve performance, or to gain some other benefit).
这是因为从理论上讲,开发人员可以选择保留自己在未来更改实施的选项,这样它就不再支持现有订单(可能是为了提高性能,或者为了获得其他好处)。
Now as a practical matter, we know that the current implementation honors the ordering -- PHP arrays are ordered containers (there is a linked list of values among other things) -- and this is something you wouldn't ever expect to change.
现在作为一个实际问题,我们知道当前的实现遵循排序 - PHP数组是有序容器(除了其他东西之外还有一个值的链接列表) - 这是你不会期望改变的。
If it did, the change would hint to a corresponding significant change in the internal implementation of arrays and that would in turn be likely to break lots of other code too. I don't see it happening any time soon.
如果确实如此,则更改将暗示数组的内部实现中的相应重大更改,这反过来可能会破坏许多其他代码。我不认为它会很快发生。
#2
2
If you are concerned, you could always pick one as the correct order and then reimplement the other function based on that. And if you care about consistency between the two calls, you probably are going to call both array_keys
and array_values
at the same time. So why not do both simultaneously? E.g., let's assume that the order of array_keys()
is "correct". Then do:
如果您担心,您可以随时选择一个作为正确的顺序,然后根据它重新实现其他功能。如果你关心两个调用之间的一致性,你可能会同时调用array_keys和array_values。那么为什么不同时做两个呢?例如,假设array_keys()的顺序是“正确的”。然后做:
function arrayKV($arr) {
$keys = array_keys($arr);
$values = array();
foreach($keys as $key) {
$newArr[] = $arr[$key];
}
return array('keys' => $keys, 'values' => $values);
}
That way, you know that they are in the same order. Alternatively, you could provide the keys to use as the order:
这样,你知道它们的顺序是一样的。或者,您可以提供用作订单的密钥:
function arrayValuesStable($arr, $keys) {
$values = array();
foreach($keys as $key) {
$values[] = $arr[$key];
}
return $values;
}