array_sum
Adds up all the values in an array and returns the total as an int or float.
array_sum(array $array): int|float- Added in PHP 4.0.4
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The array of values to add together. Keys are ignored. |
Return value
int|floatThe sum of the values. Returns integer 0 for an empty array.
Errors
Does not throw for unsummable values; since PHP 8.3 it emits an E_WARNING for each one and skips it.
Changelog
- PHP 8.3Now emits E_WARNING when a value cannot be converted to int or float. Previously arrays and objects were silently ignored while every other value was cast to int. Objects that define a numeric cast, such as GMP, are now cast instead of ignored.
array_sum() adds every value in the array together. Keys are ignored entirely, so it works the same on a list and on an associative array.
When to reach for it
It is the fastest way to total a column of numbers: cart line totals, invoice amounts, response times, vote counts. Pair it with array_column() to pull one field out of a list of records, or with array_map() when each total needs computing first. For an average, divide by count() — but guard against an empty array, because array_sum([]) / count([]) is a division by zero.
What trips people up
The return type flips between int and float depending on the inputs. Any float in the array makes the whole result a float, and summing money as floats accumulates the usual binary rounding error — store amounts in cents as integers if exactness matters. If the total exceeds PHP_INT_MAX it silently becomes a float and loses precision.
Non-numeric values are a trap. Before PHP 8.3 strings like 'abc' were cast to 0 and nested arrays were skipped without a peep. Since 8.3 each unconvertible value raises an E_WARNING, which surfaces the bad data instead of quietly producing a wrong total. Numeric strings such as '12' are still added normally.
Example
When to use it
- Total the line items of a shopping cart to get an order subtotal.
- Compute an average by dividing array_sum by count, after checking the array is not empty.
- Add up a single column pulled out of a list of database rows with array_column.
Frequently asked questions
What does array_sum return for an empty array?
It returns integer 0, not null or false. That means an average calculation still needs its own guard, because dividing by count([]) is a division by zero.
Does array_sum ignore non-numeric values?
Since PHP 8.3 it emits an E_WARNING for each value that cannot be converted to int or float and skips it. Numeric strings like '12' are still converted and added normally.
Why does array_sum return a float when all my values look like integers?
A single float value, or a total that exceeds PHP_INT_MAX, promotes the whole result to float. Cast with intval() only when you know the values are truly integral.
More examples
Summing one column of records
array_column extracts just the amount field, giving [120, 80, 45], and array_sum totals it to 245. The average guard avoids a division by zero when $rows is empty.
<?php
$rows = [
['id' => 1, 'amount' => 120],
['id' => 2, 'amount' => 80],
['id' => 3, 'amount' => 45],
];
$total = array_sum(array_column($rows, 'amount'));
echo "total: $total\n";
echo "average: ", $rows ? $total / count($rows) : 0, "\n";Floats versus integer cents
The float sum is 0.30000000000000004 and the identity check is false. Storing the same amounts as integer cents gives an exact 30, formatted back to 0.30 only at display time.
<?php
$floats = [0.1, 0.2];
var_dump(array_sum($floats));
var_dump(array_sum($floats) === 0.3);
$cents = [10, 20];
var_dump(array_sum($cents));
echo number_format(array_sum($cents) / 100, 2), "\n";Numeric strings and empty arrays
Numeric strings are converted and added, so the first call gives float(32.5). An empty array returns int(0), and keys are ignored entirely, so the associative array sums to int(12).
<?php
var_dump(array_sum(['10', '20', '2.5']));
var_dump(array_sum([]));
var_dump(array_sum(['a' => 5, 'b' => 7]));
Discussion