-2

How can I sum values of two arrays with same keys. Example:

$a = ('ox' => '5'; 'wow'=>'6')
$b = ('ox' => '15'; 'wow'=>'4')

Desired result

$c = ('ox' => '20'; 'wow'=>'10')

Thanks

Elias Soares
  • 9,884
  • 4
  • 29
  • 59
  • 1
    what have you tried? iterating across one object's keys and adding in the other's values would be a start – derelict Sep 07 '17 at 01:53
  • 2
    Possible duplicate of [How to sum values of the array of the same key?](https://stackoverflow.com/questions/1496682/how-to-sum-values-of-the-array-of-the-same-key) –  Sep 07 '17 at 01:53

1 Answers1

0

try this one:

$c = [];
foreach($a as $key => $a_){
    $c[$key] = $a_ + $b[$key];
}
parpar
  • 329
  • 1
  • 10