Reduce the array to a single value iteratively combining all values using $callback.

/**
 * Reduce the array to a single value iteratively combining all values using $callback.
 *
 * @param callable   $callback Callback with ($carry, $item)
 * @param mixed|null $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.
 */
public function reduce(callable $callback, $initial = null)

Examples

$result = Arrays::create([2, 2])
                ->reduce(function ($carry, $item) {
                    $carry += $item;
                    return $carry;
                });

print_r($result);

The above example will output:

4

Getting Started

Methods