❮ PHP Array Reference
Example
Return the sum of all the values in the array [5+15+25]:
Try it Yourself »
Definition and Usage
The array_sum[] function returns the sum of all the values in the array.
Syntax
Parameter Values
array | Required. Specifies an array |
Technical Details
Returns the sum of all the values in an array |
4.0.4+ |
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]:
Try it Yourself »
❮ PHP Array Reference
[PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8]
array_sum — Calculate 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
The above example will output:
rodrigo at adboosters dot com ¶
8 months ago
If you want to calculate the sum in multi-dimensional arrays:
Example:
samiulmomin191139 at gmail dot com ¶
7 months ago
444 ¶
6 months ago
$total = 0;
foreach [$array as $key => $value]{
$total += $value;
}
Print "sum $total";