gpt4 book ai didi

php - array_merge 是否保留顺序?

转载 作者:行者123 更新时间:2023-12-02 20:04:09 25 4
gpt4 key购买 nike

我有两个数组:

$arr = ["ham", "beef", "testing1"];
$arr1 = ["baby", "chicken", "wax"];

合并它们时我得到以下结果:

var_dump(array_merge($arr, $arr1));
// ["ham", "beef", "testing1", "baby", "chicken", "wax"]

如您所见,顺序保持不变,并将它们添加到第一个数组的末尾。我可以确定情况总是如此吗?或者顺序不一定保留?我在 the docs 中什么也没找到关于结果的顺序。

最佳答案

您可以在 documentation 中阅读更多内容:

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

正如您所看到的,第二个数组的值被附加到前一个数组的末尾。

对于重复的键,最后一个键将覆盖前一个键,如文档中所述:

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

关于php - array_merge 是否保留顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55143435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com