Php adds two arrays. Copy the code as follows :? Phpfunctionarray_add ($ a, $ B) {obtain the intersection of two arrays based on the key name $ arrarray_intersect_key ($ a, $ B); traverse the second array if the key name does not exist
The code is as follows:
Function array_add ($ a, $ B ){
// Obtain 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 element to the first array.
Foreach ($ B as $ key => $ value ){
If (! Array_key_exists ($ key, $ )){
$ A [$ key] = $ value;
}
}
// Calculate the sum of array elements with the same key name and replace the element values corresponding to the same key name in the original array
Foreach ($ arr as $ key => $ value ){
$ A [$ key] = $ a [$ key] + $ B [$ key];
}
// Returns the added array.
Return $;
}
$ A = array ('0' => '2', '1' => '4', '3' => '8 ', 'A' => '123 ');
$ B = array ('0' => '5', '2' => '4', 'B' => '33 ', 'A' => '22 ');
$ Arr = array_add ($ a, $ B );
Print_r ($ arr );
?>
The http://www.bkjia.com/PHPjc/323646.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323646.htmlTechArticle code is as follows :? Php function array_add ($ a, $ B) {// Obtain 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...