array_sum(): Addition is not supported on type array

Description

array_sum() adds each element in the first argument, with each other. This is a valid operation for integers, floats, numeric strings, booleans and null, as they can be cast to numbers.

This is not the case for arrays, objects, non-numeric strings or resources, which yield this warning, and are omitted in the operation.

Until PHP 8.3, the omission of invalid argument was a silent behavior.

Example

<?php

var_dump(array_sum([[]] )); // returns 0

var_dump(array_sum([[], 3] )); // returns 3

?>

Solutions

  • Filter out all values in the first argument not actually numeric: aka, they can’t be cast to integer or float without error.

  • Filter out all arrays inside the first argument.