[英]array_merge is not replacing the matching $key
I'm trying to figure out merging two arrays, which was originally working but now I'm trying to merge multidimensional arrays. 我试图找出合并两个数组的方法,这些方法本来可以正常工作,但是现在我试图合并多维数组。
First array: 第一个数组:
$old_array =
Array (
[2] => Array (
[a] => Location 2
[b] => loc02
[c] => Array ( )
)
[3] => Array (
[a] => Location 3
[b] => loc04
[c] => Array ( [reader] => reader )
)
)
And the second array: 第二个数组:
$new_array =
Array (
[3] => Array (
[a] => Location 3 New
[b] => loc06
[c] => Array ( [publisher] => publisher )
)
)
When I run then through the array_merge( $old_array, $new_array )
the second array just gets added to the bottom opposed to replacing the same line. 当我运行时,通过
array_merge( $old_array, $new_array )
将第二个数组添加到底部,而不是替换同一行。
This was working previously, the only change has been adding [c]
's array and not sure if the merge is causing the adding not replacement. 以前这是可行的,唯一的更改是添加
[c]
的数组,并且不确定合并是否导致添加而不是替换。
For numeric indices, array_merge will just append the new items to the end of the first array. 对于数字索引,array_merge只会将新项追加到第一个数组的末尾。 This works well for string index, as the values gets overwritten.
这对于字符串索引非常有效,因为值会被覆盖。 If you want to merge them for numeric indices, use "+" operator.
如果要合并它们以获取数字索引,请使用“ +”运算符。
Check out this comment: https://www.php.net/manual/en/function.array-merge.php#92602 查看此评论: https : //www.php.net/manual/zh/function.array-merge.php#92602
Forgot to add, if you have 2 arrays, $a
and $b
and you want to overwrite the values of $a
with values of $b
, then, $new_array = $b + $a;
忘了补充,如果你有2个阵列,
$a
和$b
,并要覆盖的值$a
带有值$b
,那么, $new_array = $b + $a;
So, the array above would result in: 因此,上面的数组将导致:
Array
(
[3] => Array
(
[a] => Location 3 new
[b] => loc06
[c] => Array
(
[publisher] => publisher
)
)
[2] => Array
(
[a] => Location 2
[b] => loc02
[c] => Array
(
)
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.