Arrays\mapKey()

higher-order transformer lazy accepts iterable returns Closure pure
(int|string → int|string) → (Iterable → Iterable)
At a glance — Maps keys, not values. Values stay the same; colliding remapped keys overwrite.

Creates a Closure that transforms the keys of an array or iterable. Setting a key to an existing index overwrites the value already there.

/**
  * @param callable $func
  * @return Closure(iterable<int|string, mixed>):(array<int|string, mixed>|\Generator<int|string, mixed>)
  */
Arrays\mapKey(callable $func): Closure

Returned Closure

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

/**
  * @param iterable<int|string, mixed> $source
  * @return array<int|string, mixed>|\Generator<int|string, mixed>
  */
$function (iterable $source): array|\Generator

Examples

Partial Application

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

$prefix = Arrays\mapKey(fn($k) => "__$k");

print_r($prefix(['a' => 1, 'b' => 2]));
// ['__a' => 1, '__b' => 2]

Curried

This can be called inline using currying.

print_r(Arrays\mapKey('strtoupper')(['a' => 1])); // ['A' => 1]

Details

Arrays Functions

Releated Array map Functions