array_sum

mixed array_sum(array array) 

Returns the sum of an array’s elements.

Returns:

Sum of the values in an array; NULL on failure

Description:

This function simply totals the numeric values of the elements of array and returns the result. If array contains other arrays or objects, they’re skipped. Non-numeric values in array are internally cast to their numeric equivalents for the addition; this casting is internal only and these values still have their original values and types after the call.

Version:

PHP 4 since 4.0.4

Example:

Sum the elements of an array
$array = array(1, 2, "3.3 kilograms", array(4)); 
echo "The sum of the elements is: " . array_sum($array) . "\n"; 

Output: 
The sum of the elements is: 6.3 

Get PHP Functions Essential Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.