Sum Values of Multidimensional Array by Key Without Loop

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 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 specific values in a multidimensional array (php)

Try below code:

<?php

$arr = array(
array('city' => 'NewYork', 'cash' => '1000'),
array('city' => 'Philadelphia', 'cash' => '2300'),
array('city' => 'NewYork', 'cash' => '2000'),
);

$newarray = array();
foreach($arr as $ar)
{
foreach($ar as $k => $v)
{
if(array_key_exists($v, $newarray))
$newarray[$v]['cash'] = $newarray[$v]['cash'] + $ar['cash'];
else if($k == 'city')
$newarray[$v] = $ar;
}
}

print_r($newarray);


Output:

Array
(
[NewYork] => Array
(
[city] => NewYork
[cash] => 3000
)

[Philadelphia] => Array
(
[city] => Philadelphia
[cash] => 2300
)

)


Demo:

http://3v4l.org/D8PME

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);

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

Sum of columns in multidimensional array without loops

You can array_reduce your array to a sum that way :

$array[0] = array('female', 2);
$array[1] = array('female', 5);
$array[2] = array('male', 2);

$sum = array_reduce($array, function ($value, $item) {
if ($item[0] == 'female') $value += $item[1];
return $value;
}, 0);

var_dump($sum);

Output :

7

Group multidimensional array by key and sum values in PHP

I would do all the necessary math in the loop that reads the data from the database. Something like this:

$ratings = array();
while ($row = $result->fetch_assoc()) {
$name = $row['name'];
if (!isset($ratings[$name])) {
$ratings[$name] = array('count' => 1, 'sum' => $row['rating']);
}
else {
$ratings[$name]['count']++;
$ratings[$name]['sum'] += $row['rating'];
}
}

Then you can just output your table like so:

echo "<table>";
foreach ($ratings as $name => $r) {
echo "<tr><td>$name</td><td>" . round($r['sum'] / $r['count'], 1) . "</td></tr>";
}
echo "</table>";


Related Topics



Leave a reply



Submit