multidimensional array array_sum
Just a way to do it:
$sum = 0;
foreach($account_invoices as $num => $values) {
$sum += $values[ 'vatAmount' ];
}
PHP Array_Sum on multi dimensional array
Just do a simple foreach
on all items and sum the values:
$values = array(
'character_length' => 0,
'word_count' => 0
);
foreach ($array as $item) {
$values['character_length'] += $item['values']['character_length'];
$values['word_count'] += $item['values']['word_count'];
}
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.
Sum values from a multidimensional array in PHP
Please, always share with us what you have tried.
It help us a lot.
You can use:
$arr = [['name' => "Banana", 'quantity' => 124], ['name' => "Cherry", 'quantity' => 24], ['name' => "Apple", 'quantity' => 224]];
$sum = 0;
foreach ($arr as $item) {
$sum += $item['quantity'];
}
Or (PHP 5.5+):
$sum = array_sum(array_column($arr, 'quantity'));
how to sum in a multidimensional array in php
I think your "array"
$Batches = array(
$Batch_1= array(
$A_Row= array(1,1,0,0),
$B_Row = array(0,0,0,1),
$C_Row = array(0,0,0,1)),
$Batch_2= array(
$A_Row= array(1,0,0,0),
$B_Row = array(0,0,0,1),
$C_Row = array(0,0,0,1))
);
is a product of the imagination. But it is also valid PHP code. I assume you want to work with keys and the array looks like this:
$Batches = array(
'Batch_1' => array(
'A_Row' => array(1,1,0,0),
'B_Row' => array(0,0,0,1),
'C_Row' => array(0,0,0,1)),
'Batch_2' => array(
'A_Row' => array(1,0,0,0),
'B_Row' => array(0,0,0,1),
'C_Row' => array(0,0,0,1))
);
The Summation works better with foreach.
$sum_arr = [];
foreach($Batches as $ib => $batch){
foreach($batch as $ir => $row){
foreach($row as $ic => $col){
$sum_arr[$ir][$ic] ??= 0;
$sum_arr[$ir][$ic] += $col;
}
}
}
var_export($sum_arr);
The Output:
array (
'A_Row' =>
array (
0 => 2,
1 => 1,
2 => 0,
3 => 0,
),
'B_Row' =>
array (
0 => 0,
1 => 0,
2 => 0,
3 => 2,
),
'C_Row' =>
array (
0 => 0,
1 => 0,
2 => 0,
3 => 2,
),
)
The summation also works with the original array. However, this array has only numeric keys.
How to find the sum of a multidimensional associative array?
Instead of nested loops, use array_sum()
and array_column()
.
foreach ($testarray[0] as $date => $results) {
$sums[$date] = array_sum(array_column($results, 'total'));
Sum each row of a multidimensional array
array_sum
needs array not values. Do like this:
for($i = 0; $i < 4; $i++) {
$sumresult[] = array_sum($number[$i]);
}
How to do a multidimensional array_sum of columns with distinct keys?
<?php
$array = [
0 => [
"2504_2512" => 309,
"2504_2513" => 654,
"2504_2514" => 234,
],
1 => [
"2505_2512" => 143,
"2505_2513" => 488,
"2505_2514" => 68,
],
2 => [
"2506_2512" => 325,
"2506_2513" => 670,
"2506_2514" => 250
],
3 => [
"2507_2512" => 263,
"2507_2513" => 608,
"2507_2514" => 188
]
];
$columnSums = [];
foreach($array as $data){
$values = array_values($data);
foreach ($values as $key => $value) {
if (empty($columnSums[$key])) {
$columnSums[$key] = $value;
} else {
$columnSums[$key] += $value;
}
}
}
print_r($columnSums);
result:
Array
(
[0] => 1040
[1] => 2420
[2] => 740
)
Just use array_values
to get a new array with values and keys like 0,1,2
PHP sum values from multidimensional array with using a for loop
You can use array_walk for example and passing $sum by reference.
<?php
$array = array (
'section1' => array (
'value' => 465,
'value2' => 744
),
'section2' => array (
'value' => 6544,
'value2' => 565
),
'section5' => array (
'value' => 345,
'value2' => 7465
)
);
$sum = 0;
array_walk($array, function($a) use (&$sum){
$sum2 += $a['value2'];
});
print_r($sum);
// 8774
PHP - How to get the sum of a multidimensional array?
You can use array_walk_recursive()
in combination with using an outer variable by reference:
$sum = 0;
array_walk_recursive($array, function($number) use (&$sum) {
$sum += $number;
});
echo $sum;
In case an element of an array is an array itself, array_walk_recursive()
will iterate through it. Otherwise it will call the function on the element.
Related Topics
Include Constant in String Without Concatenating
Chaining Static Methods in PHP
Why Are Functions and Methods in PHP Case-Insensitive
How to Install PHP-Curl in Ubuntu 16.04
Fatal Error: Class 'Ziparchive' Not Found In
Php: How to Call Function of a Child Class from Parent Class
Generate Random 5 Characters String
Php7.1 JSON_Encode() Float Issue
How to Build a JSON Array from MySQL Database
PHP MySQL Update If Exist or Insert If Not
Any Decent PHP Parser Written in PHP
How to Write a Stored Procedure Using PHPmyadmin and How to Use It Through PHP
Best Way to Determine If a Url Is an Image in PHP
How to Use PHP to Check If a Directory Is Empty