gpt4 book ai didi

php - Array_merge 与 +

转载 作者:IT老高 更新时间:2023-10-28 11:49:24 25 4
gpt4 key购买 nike

当我将 array_merge() 与关联数组一起使用时,我得到了我想要的,但是当我将它们与数字键数组一起使用时,键会改变。

使用 + 会保留键,但它不适用于关联数组。

我不明白这是怎么回事,谁能给我解释一下?

最佳答案

因为两个数组都是数字索引的,所以只会使用第一个数组中的值。

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

http://php.net/manual/en/language.operators.array.php

array_merge() 的行为略有不同:

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. Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

http://php.net/manual/en/function.array-merge.php

关于php - Array_merge 与 +,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7059721/

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