累加两个php数组,键相同的累加,不同的合并
1.
$arr1=array('name'=>33,'type'=>1);
$arr2=array('name'=>33,'price'=>200);
$res=array();
foreach($arr1 as $k=>$v){
if(isset($arr2[$k])){
$res
[
$k
] =
$arr1
[
$k
] +
$arr2
[
$k
];
unset($arr1
[
$k
]
,$arr2
[
$k
]
);
}
}
$res=array_merge($res,$arr1,$arr2);
2.
$arr1=array('name'=>33,'type'=>1);
$arr2=array('name'=>33,'price'=>200);
$res=array();
foreach($arr1 as $k=>$v){
if(isset($arr2[$k])){
$res
[
$k
] =
$arr1
[
$k
] +
$arr2
[
$k
];
}
}
$res=$res+$arr1+$arr2;