array_reduce

FunctionParams and return types changed in PHP 8.0

Iteratively reduce the array to a single value using a callback function.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

array_reduce Function synopsis

array_reduce(
    array $array,
    callable $callback,
    mixed $initial = null
  ): mixed

Parameters

$array

Typearray

The input array.

$callback

Typecallable

Holds the return value of the previous iteration; in the case of the first iteration it instead holds the value of $initial.

$initial

OptionalTypemixedDefault valuenull

If the optional $initial is available, it will be used at the beginning of the process, or as a final result in case the array is empty.

Return value

Typemixed

Returns the resulting value.

If the array is empty and $initial is not passed, array_reduce returns null.

Changes to the array_reduce Function

PHP 8.0

  • Return type added: mixed
  • Parameter name of parameter #1 changed: $arg to $array
  • Parameter type added for parameter #1 ($array): array
  • Parameter type added for parameter #2 ($callback): callable
  • Parameter type added for parameter #3 ($initial): mixed
  • Parameter default value added for position #3 ($initial): null
  array_reduce(
-     $arg,
+     array $array,
-     $callback,
+     callable $callback,
-     $initial
+     mixed $initial = null
-   )
+   ): mixed

array_reduce Function Availability

PHP VersionAvailability
PHP 8.6Upcoming Release Yes
PHP 8.5Supported (Latest) Yes
PHP 8.4Supported Yes
PHP 8.3Security-Fixes Only Yes
PHP 8.2Security-Fixes Only Yes
PHP 8.1Unsupported Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes