17

I have two arrays:

$a = array(10, 2, 5, 10, 0);
$b = array(1, 20, 11, 8, 3);

I need to sum up and get the result:

$c = array(11, 22, 16, 18, 3);

Any suggestions on how do this without "foreach"?

  • 2
    [What have you tried?](http://www.whathaveyoutried.com/) See [ask advice](http://stackoverflow.com/questions/ask-advice), please. (This is basic PHP) – John Conde Mar 21 '13 at 13:34
  • 1
    You need to add each element of the `$a` to a corresponding element of the array `$b` and get the result. – Voitcus Mar 21 '13 at 13:35
  • http://stackoverflow.com/questions/1496682/how-to-sum-values-of-the-array-of-the-same-key – user0103 Mar 21 '13 at 13:36
  • I want to know the best method. I know how to do this via loop. –  Mar 21 '13 at 14:26
  • I Used array_merge_recursive() to merge recursively all arrays into one and i splitted every inner arrays using foreach and used array_sum() to sum the inner arrays. Now i have only one array with all arrays added. This is only for arrays with same index! – Aravindh Gopi Nov 23 '16 at 11:50
  • It is important for answerers to understand if the two indexed arrays will also have the same size or if one might be longer than the other. – mickmackusa Jan 06 '23 at 10:09

4 Answers4

43

a simple approach could be

$c = array_map(function () {
    return array_sum(func_get_args());
}, $a, $b);

print_r($c);

Or if you could use PHP5.6, you could also use variadic functions like this

$c = array_map(function (...$arrays) {
    return array_sum($arrays);
}, $a, $b);

print_r($c);

Output

Array
(
    [0] => 11
    [1] => 22
    [2] => 16
    [3] => 18
    [4] => 3
)
Philipp
  • 15,377
  • 4
  • 35
  • 52
  • 3
    You don't need PHP 7 for the second approach. Argument unpacking (http://php.net/manual/en/migration56.new-features.php#migration56.new-features.splat) is available since PHP 5.6. – flu Apr 27 '17 at 15:22
  • @flu thanks for the hint - just tested it and updated my answer – Philipp Apr 27 '17 at 15:31
  • 1
    From http://php.net/manual/en/function.array-map.php : "The returned array will preserve the keys of the array argument if and only if exactly one array is passed. If more than one array is passed, the returned array will have sequential integer keys." This means that array keys are not preserved. – Kyborek Nov 16 '18 at 09:44
  • My two favourite answers. The variadic solution being my favourite. Indeed, it leads to the next step : appending $a and $b as elements of another array say, $x and then adding ...$x as second argument of array_map. It allows array_map to work with an undefined number of arrays.Not really required but very useful when working with datasets – Bevelopper Oct 06 '20 at 12:10
16

Try like

$c = array();
foreach (array_keys($a + $b) as $key) {
    $c[$key] = $a[$key] + $b[$key];
}
GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • 1
    While not specifically asked in this question. This solution (unlike other answers) preserves array keys and even deals with different-length or different-keys arrays – Kyborek Nov 16 '18 at 09:46
8

There's no way you can do that without the "foreach" as you asked for, you need to loop on both arrays to get the respective values.

A function using the foreach would be :

function sum_arrays($array1, $array2) {
    $array = array();
    foreach($array1 as $index => $value) {
        $array[$index] = isset($array2[$index]) ? $array2[$index] + $value : $value;
    }
    return $array;
}

Now you just need to do :

$c = sum_arrays($a, $b);
user1997620
  • 207
  • 4
  • 7
1
$c = array();
for($i=0;$i<count($a);$i++) {
  $c[$i] = $a[$i]+$b[$i];
}
Samuel Bolduc
  • 18,163
  • 7
  • 34
  • 55
  • As basic as this question and solution may be, please always include some explanation with every answer. Tell the OP and future researchers how your code works and why it is advisable. – mickmackusa Aug 15 '18 at 20:24