Arrays\sumWhere()

higher-order reducer terminal accepts iterable returns Closure pure
(T → number) → (Iterable → number)
At a glance — Map + sum in one pass. Terminal — consumes the whole source.

Creates a Closure that sums the result of applying a callback to each element of an array or iterable — like mapping-then-summing in a single pass.

/**
  * @param callable(mixed):(int|float) $function Applied to each value before summing.
  * @return Closure(iterable<int|string, mixed>):(int|float)
  */
Arrays\sumWhere(callable $function): Closure

Returned Closure

When Arrays\sumWhere() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param iterable<int|string, mixed> $source
  * @return int|float
  */
$function (iterable $source)

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

$totalAge = Arrays\sumWhere(fn($p) => $p['age']);

echo $totalAge([['age' => 20], ['age' => 30], ['age' => 25]]); // 75

Curried

This can be called inline using currying.

echo Arrays\sumWhere(fn($n) => $n * 2)([1, 2, 3]); // 12

Details

Arrays Functions

Releated Array fold Functions