Function Code for adding two arrays in php. For more information, see.
Function Code for adding two arrays in php. For more information, see.
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 );
?>