fold but with access to the key at each step. Terminal; streams the source via foreach.
Creates a Closure that reduces an array or iterable left-to-right with the callback receiving each (carry, key, value) triple.
/**
* @param callable(mixed $carry, int|string $key, mixed $value): mixed $callable
* @param mixed $initial
* @return Closure(iterable<int|string, mixed>):mixed
*/
Arrays\foldKeys(callable $callable, $initial = []): ClosureWhen Arrays\foldKeys() is called, it returns the following Closure which can be used like a regular function.
/**
* @param iterable<int|string, mixed> $source
* @return mixed
*/
$function (iterable $source)This can be used to create a simple closure which can be used as a regular function.
$keyValuePairs = Arrays\foldKeys(
fn($acc, $k, $v) => $acc . "$k=$v; ",
''
);
echo $keyValuePairs(['a' => 1, 'b' => 2]); // a=1; b=2;