how to add values of two arrays with common index in php? -
this question has answer here:
- how sum values of array of same key? 13 answers
consider following arrays:
array 1 contents:
index1 value2 5 b 6 c 2
and array 2 contents:
index2 value2 4 b 2
i need array 3 should sum of above 2 arrays. like:
index value 9 b 8 c 2
this function need:
function array_merge_with_sum_values(array $array1, array $array2){ $finalarray = array_merge($array1, $array2); $keys = array_keys($finalarray); foreach($keys $key){ if(isset($array1[$key]) && isset($array2[$key])){ $finalarray[$key] = $array1[$key] + $array2[$key]; } } return $finalarray; }
Comments
Post a Comment