Arrays7 min readExamples run on PHP 8.5.10

array_sum()

Learn how PHP 8.4 array_sum() adds array values, what it returns for empty arrays and mixed types, and which mistakes to avoid.

Signature

array_sum(array $array): int|float
Available since PHP 4.0.47.48.08.18.28.38.48.5All versions →

Purpose

array_sum() walks through an array and adds every value together. It is the quickest way to total a list of quantities, prices in cents, scores or counters without writing a foreach loop and an accumulator variable yourself. The keys of the array play no part in the calculation; only the values are added.

Signature

Parameters and defaults

  • $array: the input array whose values are added. It is the only parameter and it is required, so there are no defaults to remember.

The function does not modify the array you pass in. It works with both list-style and associative arrays.

Return value and failure cases

The result is an int or a float. If every value is an integer (or converts to one) and the total stays in range, you get an int. As soon as a float takes part, the result is a float. An empty array gives 0.

Passing something that is not an array, such as a string or null, raises a TypeError, because the parameter is declared as array.

Since PHP 8.3 the function emits an E_WARNING when array values cannot be converted to an integer or a float. Before that version, arrays and objects inside the input were silently ignored. Objects that define a numeric cast, such as GMP objects, are now cast rather than ignored. In practice this means you should validate data before summing it instead of relying on the function to skip bad entries.

Example 1: integers, floats and the empty array

The first example shows the three most common result shapes. The float values are chosen so they can be represented exactly in binary, which keeps the output predictable.

Example
<?php
var_dump(array_sum([2, 4, 6, 8]));
var_dump(array_sum([0.5, 1.25, 2.25]));
var_dump(array_sum([]));
Output
int(20)
float(4)
int(0)

The integers produce an int, the floats produce a float, and the empty array produces the integer 0.

Example 2: keys are ignored and numeric values are converted

Associative keys do not matter. Numeric strings are converted using normal numeric rules, and booleans and null take part as 1, 1 for true and 0 for false or null.

Example
<?php
var_dump(array_sum(["x" => 10, "y" => 5]));
var_dump(array_sum(["3", "4", "5"]));
var_dump(array_sum(["3", "4.5", true, null]));
Output
int(15)
int(12)
float(8.5)

The middle call returns an integer because every string is an integer string. The last call returns a float because "4.5" is a float string.

Example 3: totalling computed line items

array_sum() pairs well with array_map(). Here each item has a unit price in cents and a quantity. Working in whole cents avoids rounding surprises that appear with fractional currency values.

Example
<?php
$cart = [
    ["name" => "Pen", "cents" => 150, "qty" => 4],
    ["name" => "Notebook", "cents" => 425, "qty" => 2],
    ["name" => "Eraser", "cents" => 75, "qty" => 1],
];

$lineTotals = array_map(
    fn(array $item): int => $item["cents"] * $item["qty"],
    $cart
);

echo json_encode(
    ["lines" => $lineTotals, "total" => array_sum($lineTotals)],
    JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
), "\n";
Output
{"lines":[600,850,75],"total":1525}

Edge cases

  • Empty input: the result is the integer 0, never null or false. This makes it safe as a starting total.
  • Integer overflow: when an integer sum goes beyond the platform's integer range, PHP continues with a float, so the result type can change with large data.
  • Floating-point precision: values such as 0.1 and 0.2 cannot be stored exactly, so a total may differ from what you expect in the last digits. Compare with a tolerance, or use integer cents.
  • Duplicates: every element counts. If you want each distinct value once, apply array_unique() first.
  • Nested arrays: the function does not recurse. A nested array is an unsupported value, not a group to be totalled. Flatten or sum the inner arrays first.

Common mistakes

  1. Expecting a count. array_sum() adds values; it does not count elements. Use count() for that.
  2. Summing an array column directly. For a list of records, extract the numbers first with array_column() or array_map(), as the third example does.
  3. Feeding it unclean input. Text such as "12 apples" or nested arrays are not clean numbers. Since PHP 8.3 unconvertible values trigger a warning, so filter or validate form and CSV data before summing.
  4. Assuming the result is always a float or always an int. Check the type if it matters, for instance before formatting with number_format().
  5. Using floats for money. Prefer integer cents and format at the edge of your program.
  • array_product() multiplies values instead of adding them.
  • count() returns the number of elements.
  • array_map() and array_column() prepare the numbers that you want to total.
  • array_reduce() handles custom accumulation, such as weighted totals.
  • max() and min() find extremes rather than totals.

Recap

array_sum(array $array): int|float adds all values of an array, ignores keys, and returns 0 for an empty array. Integers stay integers until a float or an overflow forces a float. Since PHP 8.3 values that cannot be converted to numbers raise a warning, so clean your data first, and keep money in integer cents for reliable totals.

Tested examples

Integers, floats and the empty array
<?php
var_dump(array_sum([2, 4, 6, 8]));
var_dump(array_sum([0.5, 1.25, 2.25]));
var_dump(array_sum([]));
Output
int(20)
float(4)
int(0)
Keys are ignored and numeric values are converted
<?php
var_dump(array_sum(["x" => 10, "y" => 5]));
var_dump(array_sum(["3", "4", "5"]));
var_dump(array_sum(["3", "4.5", true, null]));
Output
int(15)
int(12)
float(8.5)
Totalling computed line items
<?php
$cart = [
    ["name" => "Pen", "cents" => 150, "qty" => 4],
    ["name" => "Notebook", "cents" => 425, "qty" => 2],
    ["name" => "Eraser", "cents" => 75, "qty" => 1],
];

$lineTotals = array_map(
    fn(array $item): int => $item["cents"] * $item["qty"],
    $cart
);

echo json_encode(
    ["lines" => $lineTotals, "total" => array_sum($lineTotals)],
    JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
), "\n";
Output
{"lines":[600,850,75],"total":1525}