Unset&#x2B之后,不一致的数组键;合并+添加值,版本7.3& 7.4
代码示例:
<?php
$values = []; // start values
$list = ['one', 'two', 'three']; // some list
$oneMore = 'four'; // some element
unset($list[2]); // unset last(!) list element
$values = array_merge($values, $list); // add list to values
$values[] = $oneMore; // add element to values
print_r($values);
7.3.33的结果:
Array
(
[0] => one
[1] => two
[2] => four
)
7.4.29的结果:
Array
(
[0] => one
[1] => two
[3] => four
)
如您所见,键在7.3和7.4中是不同的。
发生这种情况 $ list
数组的最后一个元素不设。如果其他一些元素不设置,则结果是相同的。
问题:在7.4+中,这种预期/所需的行为是吗?还是错误?
注意:如果在合并期间添加空数组,则结果阵列 will sytentical,ie:
$values = array_merge($values, $list, []);
Code example:
<?php
$values = []; // start values
$list = ['one', 'two', 'three']; // some list
$oneMore = 'four'; // some element
unset($list[2]); // unset last(!) list element
$values = array_merge($values, $list); // add list to values
$values[] = $oneMore; // add element to values
print_r($values);
Result for 7.3.33:
Array
(
[0] => one
[1] => two
[2] => four
)
Result for 7.4.29:
Array
(
[0] => one
[1] => two
[3] => four
)
As you see, the keys are different in 7.3 and 7.4.
This happenes only when last element of $list
array is unset. If some other element is unset, the result is identical.
Question: Is this expected/desired behaviour in 7.4+? Or a bug?
Note: If an empty array is added during merge, the resulting arrays will be indentical, i.e.:
$values = array_merge($values, $list, []);
https://sandbox.onlinephpfunctions.com/c/9625fee6-d127-45f1-8c68-3c64bcb87d0c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论