PHPize Online / SQLize Online

A A A
Share   Donate   Blog   Popular

array_reduce

Tags: PHP 5.x PHP 7.x PHP 8.x

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

Description

array_reduce(
    array $array,
callable $callback,
mixed $initial = null
): mixed
array_reduce() applies iteratively the callback function to the elements of the array, so as to reduce the array to a single value.

Parameters

array
The input array.
callback
The callback function. callback(mixed $carry, mixed $item): mixed
initial
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 Values

Returns the resulting value.
If the array is empty and initial is not passed, array_reduce() returns null.

array_reduce - New snippets