我有两个具有相同键的多维数组。我想将这些数组合并为一个。$sourceData = $array1;$sourceDataN = $array2;foreach ($sourceData as $keySd => $sd){ foreach ($sourceDataN as $keySdn => $sdn){ if($keySd == $keySdn){ foreach ($sd as $keySdd => $sdd ){ foreach ($sdn as $keySdnn => $sdnn ){ if($keySdd == $keySdnn){ $new[$keySd][] = array_merge($sdd, $sdnn); } } } } } }Array ( [809] => Array ( [ss] => Array ( [qty] => 0 ) [bh] => Array ( [qty] => 14 ) [li] => Array ( [qty] => 0 ) [mg] => Array ( [qty] => 0 ) ) [1944] => Array ( [ss] => Array ( [qty] => 25 ) [bh] => Array ( [qty] => 10 ) [za] => Array ( [qty] => 10 ) [li] => Array ( [qty] => 39 ) [mg] => Array ( [qty] => 0 ) ))Array ( [809] => Array ( [ss] => Array ( [price] => 603.07 ) [bh] => Array ( [price] => 649.95 ) [li] => Array ( [price] => 628.48 ) [mg] => Array ( [price] => 629 ) ) [1944] => Array ( [ss] => Array ( [price] => 848.7 ) [bh] => Array ( [price] => 883.74 )
1 回答
慕姐4208626
TA贡献1852条经验 获得超7个赞
您不需要全部循环 - 只需第一个数组,然后使用第二个数组的键。
您可以使用简单的嵌套foreach循环作为:
$res = [];
foreach($array1 as $k => $v) { // $k here are the number
foreach($v as $kk => $vv) { // inner arrays - $kk as 2 char
$res[$k][$kk] = array_merge($array1[$k][$kk], $array2[$k][$kk]);
}
}
- 1 回答
- 0 关注
- 161 浏览
添加回答
举报
0/150
提交
取消