How do you sum an associative array in php?

Is there a php function that returns the sum of a row of an associative array?

If not should I just use a counter and a foreach loop?

Appreciate it!

asked May 18, 2010 at 1:10

How do you sum an associative array in php?

1

To get the sum based on a certain column key, use this:

array_sum(array_column($assoc_array, 'key_name'));

answered Sep 4, 2014 at 17:52

alexalex

9362 gold badges9 silver badges11 bronze badges

1

array_sum will work for you.

$arr = array(
     'key1' => 54.3,
     65 => 10
);
$sum = array_sum($arr);

answered May 18, 2010 at 1:12

How do you sum an associative array in php?

Tim CooperTim Cooper

153k37 gold badges320 silver badges272 bronze badges

According to alex's post, you can use array_column() only if you're using PHP >= 5.5!

If you can't change the PHP-Version and your PHP-Version is lower than 5.5, you could also go for:

array_sum(array_map(function($element){return $element['key_name'];}, $assoc_array));

this will results the same.

answered Feb 27, 2015 at 23:02

How do you sum an associative array in php?

DwzaDwza

6,4174 gold badges38 silver badges69 bronze badges

array_sum http://php.net/array_sum

It sums an array - regardless of index type.

answered May 18, 2010 at 1:12

Mitch DempseyMitch Dempsey

37.5k6 gold badges65 silver badges73 bronze badges

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

array_sumCalculate the sum of values in an array

Description

array_sum(array $array): int|float

Parameters

array

The input array.

Return Values

Returns the sum of values as an integer or float; 0 if the array is empty.

Examples

Example #1 array_sum() examples

$a = array(2468);
echo 
"sum(a) = " array_sum($a) . "\n";$b = array("a" => 1.2"b" => 2.3"c" => 3.4);
echo 
"sum(b) = " array_sum($b) . "\n";
?>

The above example will output:

rodrigo at adboosters dot com

7 months ago

If you want to calculate the sum in multi-dimensional arrays:

function array_multisum(array $arr): float {
   
$sum = array_sum($arr);
    foreach(
$arr as $child) {
       
$sum += is_array($child) ? array_multisum($child) : 0;
    }
    return
$sum;
}
?>

Example:

$data =
[
   
'a' => 5,
   
'b' =>
    [
       
'c' => 7,
       
'd' => 3
   
],
   
'e' => 4,
   
'f' =>
    [
       
'g' => 6,
       
'h' =>
        [
           
'i' => 1,
           
'j' => 2
       
]
    ]
];

echo

array_multisum($data);//output: 28
?>

samiulmomin191139 at gmail dot com

7 months ago

//you can also sum multidimentional arrays like this;function arraymultisum(array $arr){
$sum=null;

        foreach(

$arr as $child){
       
$sum+=is_array($child) ? arraymultisum($child):$child;
    }
    return
$sum;
}

echo

arraymultisum(array(1,4,5,[1,5,8,[4,5,7]]));//Answer Will be
//40
?>

444

6 months ago

$total = 0;
foreach ($array as $key => $value){
   $total += $value;
}
Print "sum $total";

How do you add an associative array?

Use the array_merge() Function to Add Elements at the Beginning of an Associative Array in PHP. To add elements at the beginning of an associative, we can use the array union of the array_merge() function.

How do you sum an array value?

Algorithm.
Declare and initialize an array..
The variable sum will be used to calculate the sum of the elements. Initialize it to 0..
Loop through the array and add each element of array to variable sum as sum = sum + arr[i]..

What is an associative array in PHP give example?

Associative arrays are used to store key value pairs. For example, to store the marks of different subject of a student in an array, a numerically indexed array would not be the best choice.

How do you sum numbers in PHP?

To find sum of digits of a number just add all the digits. For example, 14597 = 1 + 4 + 5 + 9 + 7. 14597 = 26..
Take the number..
Divide the number by 10..
Add the remainder to a variable..
Repeat the process until remainder is 0..