Copy code The code is as follows:
function array_add($a,$b){
/ /Get the intersection of two arrays based on the key name
$arr=array_intersect_key($a, $b);
//Traverse the second array, if the key name does not exist with the first array, add the array elements Add to the first array
foreach($b as $key=>$value){
if(!array_key_exists($key, $a)){
$a[$key]=$ value;
}
}
//Calculate the sum of array elements with the same key name, and replace the element value corresponding to the same key name in the original array
foreach($arr as $key=> $value){
$a[$key]=$a[$key]+$b[$key];
}
//Return the added array
return $a;
}
$a = array('0'=>'2','1'=>'4','3'=>'8','a'=>'100 ');
$b = array('0'=>'5','2'=>'4','b'=>'33','a'=>'22' );
$arr=array_add($a,$b);
print_r($arr);
?>
http://www.bkjia.com/PHPjc/323646.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323646.htmlTechArticleCopy the code as follows: ?php function array_add($a,$b){ //Get two keys based on the key name Intersection of two arrays $arr=array_intersect_key($a, $b); //Traverse the second array, if the key name does not exist...