How to Sum All Column Values in Multi-Dimensional Array

Sum values of multidimensional array by key without loop

You need to couple it with array_map() to select the f_count column first:

array_sum(array_map(function($item) { 
return $item['f_count'];
}, $arr));

Nowadays you can replace the inner function with array_column():

array_sum(array_column($arr, 'f_count'));

Of course, internally, this performs a double loop; it's just that you don't see it inside the code. You could use array_reduce() to get rid of one loop:

array_reduce($arr, function(&$res, $item) {
return $res + $item['f_count'];
}, 0);

However, if speed is the only interest, foreach remains the fastest:

$sum = 0;
foreach ($arr as $item) {
$sum += $item['f_count'];
}

This is thanks to the "locality" of the variables that you're using, i.e. there are no function calls used to calculate the final sum.

How to sum all column values in multi-dimensional array?

$sumArray = array();

foreach ($myArray as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}

print_r($sumArray);

Calculate the sum of multidimensional array with same index or different indexes

Try the following example:

$sum = [];

foreach($a as $item) {
foreach($item as $key => $value) {
if (!isset($sum[$key])) $sum[$key] = 0;
$sum[$key] += $value;
}
}

print_r($sum);

If all items have the same keys:

$sum = array_fill_keys(array_keys(reset($a)), 0);

foreach($sum as $key => &$value) {
$value = array_sum(array_column($a, $key));
}

print_r($sum);

How to Sum Columns of a Multi-Dimensional Array?

There's always custom code:

function array_sum_rows($arr) {
$out = array();
foreach ($arr as $row) {
foreach ($row as $i => $val) {
$out[$i] = isset($out[$i]) ? $out[$i] + $val : $val;
}
}
return $out;
}

php function to sum values from multi dimensional array

As always, no need to over-engineer, if you know the structure, just iterate trough the values, and sum them up, this is how I would do that.

This way you can add many other marketplace and dates without later modifying the code.

<?php
$results = [
'2020-09-06' => [
'Etsy' => ['total_orders' => 2, 'total_stickers' => 3, 'total_value' => 7.8300000000000001],
'Woo' => ['total_orders' => 10, 'total_stickers' => 20, 'total_value' => 100.38],
'eBay' => ['total_orders' => 17, 'total_stickers' => 18, 'total_value' => 67.359999999999999],
],
'2020-09-07' => [
'Etsy' => ['total_stickers' => 8, 'total_orders' => 4, 'total_value' => 34.920000000000002],
'Woo' => ['total_stickers' => 9, 'total_orders' => 3, 'total_value' => '52.90'],
'eBay' => ['total_stickers' => 23, 'total_orders' => 21, 'total_value' => 58.030000000000001],
]
];

$total = ['total_stickers' => 0, 'total_orders' => 0, 'total_value' => 0];

foreach ($results as $k => $v){
foreach ($v as $k1 => $v1){
$total['total_stickers'] += $v1['total_stickers'];
$total['total_orders'] += $v1['total_orders'];
$total['total_value'] += $v1['total_value'];

}
}

var_dump($total);
/*
* array(3) {
["total_stickers"]=>
int(81)
["total_orders"]=>
int(57)
["total_value"]=>
float(321.42)
}
* */

sum of all columns in a two dimensional array python

You need to unpack the argument to zip first.

a = [[11.9, 12.2, 12.9],
[15.3, 15.1, 15.1],
[16.3, 16.5, 16.5],
[17.7, 17.5, 18.1]]
result = [sum(x) for x in zip(*a)]
>>> [61.2, 61.3, 62.6]


Related Topics



Leave a reply



Submit