PHP 8 reference · Array Functions
PHP array_sum() Function
Return the sum of the values in an array. Numeric strings are converted; an empty array gives 0.
PHP 8 Since 4.0
Syntax
array_sum(array $array): int|float
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
Return value
The sum as an int or float.
Examples
<?php
echo array_sum([1, 2, 3.5]), "\n";
echo array_sum([]), "\n";
Output
6.5 0
<?php
$cart = [["price" => 3.5], ["price" => 12]];
echo array_sum(array_column($cart, "price")), "\n";
Output
15.5
Version history
In other languages
sum()· Python
Related
array_product()· Return the product of the values in an array, multiplying them all together. An empty array gives 1.array_reduce()· Reduce an array to a single value by passing a running total and each element to a callback.array_column()· Return the values from one column of a list of records, optionally keyed by another column.- PHP Array Functions · tutorial
- PHP Arrays · tutorial