How to get the sum in php

❮ PHP Array Reference

Example

Return the sum of all the values in the array (5+15+25):

$a=array(5,15,25);
echo array_sum($a);
?>

Try it Yourself »


Definition and Usage

The array_sum() function returns the sum of all the values in the array.


Syntax

Parameter Values

ParameterDescription
array Required. Specifies an array

Technical Details

Return Value:Returns the sum of all the values in an array
PHP Version:4.0.4+
PHP Changelog:PHP versions prior to 4.2.1 modified the passed array itself and converted strings to numbers (which often converted them to zero, depending on their value)

More Examples

Example

Return the sum of all the values in the array (52.2+13.7+0.9):

$a=array("a"=>52.2,"b"=>13.7,"c"=>0.9);
echo array_sum($a);
?>

Try it Yourself »


❮ PHP Array Reference


(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

8 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 sum in PHP?

PHP | array_sum() Function The array_sum() function returns the sum of all the values in an array(one dimensional and associative). It takes an array parameter and returns the sum of all the values in it. The only argument to the function is the array whose sum needs to be calculated.

How do you sum values in an array?

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]..

How can I get the sum of elements in the array found between two integers using PHP?

php $array = array(1,2,3,4,5,6,7,8,9,10); $count = count($array); $sum = 0; for($i=0;$i<$count;$i++){ $sum += $array[$i]; } echo $sum ; ?>

How do you sum values of an array with the same key in PHP?

Use the PHP array_reduce() function to GROUP BY and SUM values of an array in PHP. In this example, we will show you how to group array by key and sum values using PHP. It helps to sum the values of an array with the same keys in PHP.