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 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.
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.
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.
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 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 column of a 2d array
This should work for arrays like the one of your example ($arr
is an array like the one in your example, I haven't defined it here for simplicity's sake):
$res = array();
foreach($arr as $value) {
foreach($value as $key => $number) {
(!isset($res[$key])) ?
$res[$key] = $number :
$res[$key] += $number;
}
}
print_r($res);
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
Related Topics
Pdo Returning Error "Could Not Find Driver" with a Known Working Dsn
How to Put a Translation System in PHP Website
Pass Value from HTML Form to PHP Without Submitting the Form
Symfony: How to Refresh the Authenticated User from the Database
Call to Undefined Function Session_Register()
How to Verify the Requesting Server in PHP
PHP Remove Commas from Numeric Strings
Multiple Radio Button Array for PHP Form
Executing Git Commands via PHP
How to Disable or Encrypt "View Source" for My Site
PHP Twitter API - How to Pull in Multiple Users Tweets
How to Convert an Integer to an Array in PHP
Sort Array by Value Alphabetically PHP
How to Recursively Obtain the "Parent Id" of Rows in This MySQL Table
Why Doesn't Sprintf() Output Anything