[英]Merge arrays with matching keys php
Ok, so someone is probably going to laugh when they read this, but I cannot get it to work. 好的,所以当他们阅读本文时,可能有人会笑,但是我无法使它起作用。 I have two arrays.
我有两个数组。
Array 1 looks like: 数组1看起来像:
Array
(
[525133-004-TURQ/WHT-9] => Array
(
[classId] => 48
[family] => Mens Shoes
[onOrder] => 3.000
[cost] => 45.000
[sell] => 95.000
[desc] => PAUL RODRIGUEZ 6, TURQ/WHT, 9
[invStore] => 0.000
[code] => 525133-004-TURQ/WHT-9
)
)
Array 2 looks like: 数组2看起来像:
Array
(
[525133-004-TURQ/WHT-9] => Array
(
[inv] => 0.000
)
)
The result needed is: 所需的结果是:
Array
(
[525133-004-TURQ/WHT-9] => Array
(
[classId] => 48
[family] => Mens Shoes
[onOrder] => 3.000
[cost] => 45.000
[sell] => 95.000
[desc] => PAUL RODRIGUEZ 6, TURQ/WHT, 9
[invStore] => 0.000
[code] => 525133-004-TURQ/WHT-9
[inv] => 0.000
)
)
I tried merge and it is not working. 我尝试了合并,但无法正常工作。 Please help!
请帮忙!
Use array_merge_recursive
: 使用
array_merge_recursive
:
$arr1 = array(
'525133-004-TURQ/WHT-9' => array(
'classId' => 48,
'family' => 'Mens Shoes',
'onOrder' => 3.000,
'cost' => 45.000,
'sell' => 95.000,
'desc' => 'PAUL RODRIGUEZ 6, TURQ/WHT, 9',
'invStore' => 0.000,
'code' => '525133-004-TURQ/WHT-9'
)
);
$arr2 = array(
'525133-004-TURQ/WHT-9' => array(
'inv' => 0.000
)
);
$newArray = array_merge_recursive($arr1, $arr2);
See it here in action: http://viper-7.com/jq8CgM 在此处查看实际操作: http : //viper-7.com/jq8CgM
foreach ($arr1 as $key => $v1) {
if (isset($arr2[$key])) {
foreach ($arr2[$key] as $k => $v) {
$arr1[$key][$k] = $v;
}
}
}
If i'm correct. 如果我是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.