将arr2的数据汇总至arr1中,必须key结构相等 如果key指向的是数组将数组里的元素进行合并
直接上代码
function mergeData(&$arr1,$arr2){
foreach($arr1 as $kk=>$vv){
process($arr1,$arr2,$kk);
}
}
function process(&$arr1,$arr2,$key){
if(is_array($arr1[$key])){
foreach($arr1[$key] as $k=>$v){
process($arr1[$key],$arr2[$key],$k);
}
}else{
$arr1[$key] = $arr1[$key]+$arr2[$key];
}
}
$arr1=[
"a"=>1,
"b"=>10,
"c"=>[
"c1"=>20,
"c2"=>30,
"c3"=>40,
],
"d"=>[
"d1"=>[
"d11"=>100,
"d12"=>200
],
"d2"=>1000,
],
"e"=>[
"e1"=>[
"e11"=>[
"e111"=>123456
]
],
"e2"=>11111
]
];
$arr2=[
"a"=>1,
"b"=>10,
"c"=>[
"c1"=>20,
"c2"=>30,
"c3"=>40,
],
"d"=>[
"d1"=>[
"d11"=>100,
"d12"=>200
],
"d2"=>1000,
],
"e"=>[
"e1"=>[
"e11"=>[
"e111"=>123456
]
],
"e2"=>11111
]
];
mergeData($arr1,$arr2);
var_dump($arr1);
|