map but the callback also sees the key. Output keys are re-indexed numerically — original keys are passed in, not preserved.
Creates a Closure for mapping an array or iterable with access to both value and key in the callback. For parity with the underlying array_map(two-arrays) path the result always has sequential integer keys.
/**
* @param callable(mixed $value, int|string $key):mixed $func
* @return Closure(iterable<int|string, mixed>):(array<int, mixed>|\Generator<int, mixed>)
*/
Arrays\mapWithKey(callable $func): ClosureWhen Arrays\mapWithKey() is called, it returns the following Closure which can be used like a regular function.
/**
* @param iterable<int|string, mixed> $source
* @return array<int, mixed>|\Generator<int, mixed>
*/
$function (iterable $source): array|\GeneratorThis can be used to create a simple closure which can be used as a regular function.
$tag = Arrays\mapWithKey(fn($value, $key) => "$key=$value");
print_r($tag(['a' => 1, 'b' => 2]));
// [0 => 'a=1', 1 => 'b=2']This can be called inline using currying.
print_r(Arrays\mapWithKey(fn($v, $k) => "$k:$v")(['x' => 10, 'y' => 20]));
// [0 => 'x:10', 1 => 'y:20']