I have multiple array like
$arr = [10, 20, 30, 40];
$arr2 = [10, 20, 30, 40];
... etc
I need to do sum of their column like
$new = [20, 40, 60, 80]
I have tried with array_sum
but it return sum of the rows
I have multiple array like
$arr = [10, 20, 30, 40];
$arr2 = [10, 20, 30, 40];
... etc
I need to do sum of their column like
$new = [20, 40, 60, 80]
I have tried with array_sum
but it return sum of the rows
array_sum
is the right approach, but you'll first need to transpose your two arrays (that is, convert the rows into columns). The easiest way to do this in PHP is to pass null as the first parameter to array_map
:
$input = array_map(null, $arr, $arr2);
(This behaviour is described in the PHP docs here: https://www.php.net/manual/en/function.array-map.php)
Once your array is transposed, you can map array_sum
to each row to achieve your desired result:
print_r(array_map('array_sum', $input));
Array ( [0] => 20 [1] => 40 [2] => 60 [3] => 80 )
See https://3v4l.org/AJtZa for a full example
Possibly, you might be interested to see how to build the logic of summing the columns of a range of arrays yourself (i.e. not depend on the magic of in-built PHP functions):
<?php
$arr = [10, 20, 30, 40];
$arr1 = [10, 20, 30, 40];
$arr2 = [10, 20, 30, 40];
$sum = sumCols($arr, $arr1, $arr2);
function sumCols(...$arrays)
{
$sumCols = [];
foreach ($arrays as $arr) {
foreach($arr as $key => $value) {
(isset($sumCols[$key])) ? $sumCols[$key] += $value : $sumCols[$key] = $value;
}
}
return $sumCols;
}
Output $sum
:
Array
(
[0] => 30
[1] => 60
[2] => 90
[3] => 120
)
A simple solution that accepts any number of arrays as input.
$arr1 = [1, 2, 3, 4];
$arr2 = [10, 20, 30, 40];
$arr3 = [100, 200, 300, 400];
$result = array_map(
fn(...$i) => array_sum($i),
$arr1,
$arr2,
$arr3
// more arrays can be added here
);
It works fine in PHP 7.4 or newer.
Check it online.
PHP 7.4 is required above for the arrow function.
However, using an arrow function is not mandatory. A classical anonymous function can be used instead:
$arr1 = [1, 2, 3, 4];
$arr2 = [10, 20, 30, 40];
$arr3 = [100, 200, 300, 400];
$result = array_map(
function(... $i) { return array_sum($i); },
$arr1,
$arr2,
$arr3
);
This version works with PHP 5.6 or newer.
Check it online.
PHP 5.6 is needed above to use ...
for the variable-length argument list.
However, it is not required either. The function func_get_args()
can be used instead to get the list of arguments:
$arr1 = [1, 2, 3, 4];
$arr2 = [10, 20, 30, 40];
$arr3 = [100, 200, 300, 400];
$result = array_map(
function() { return array_sum(func_get_args()); },
$arr1,
$arr2,
$arr3
);
This version works with PHP 5.4 or newer.
Check it online.
PHP 5.4 is needed above for the short array syntax.
However, it is not required either. The old, long syntax for arrays can be used instead:
$arr1 = array(1, 2, 3, 4);
$arr2 = array(10, 20, 30, 40);
$arr3 = array(100, 200, 300, 400);
$result = array_map(
function() { return array_sum(func_get_args()); },
$arr1,
$arr2,
$arr3
);
This version works with PHP 5.3 and newer.
Check it online.
PHP 5.3 is needed above for the anonymous function. It is not needed either.
The anonymous function can be replaced by a function created using create_function()
and this makes the code compatible even with PHP 4.
However, besides being completely useless, the old create_function()
function has been deprecated in PHP 7.2 and completely removed in PHP 8.0 for good reasons.
But a regular function can be used instead of an anonymous function:
$arr1 = array(1, 2, 3, 4);
$arr2 = array(10, 20, 30, 40);
$arr3 = array(100, 200, 300, 400);
$result = array_map('add_values', $arr1, $arr2, $arr3);
function add_values() {
return array_sum(func_get_args());
}
This version works with PHP 4.0 or newer.
PHP 4.0 has been released 21 years ago (in May 2000.)
Check it online.