Arrays\filterKey()

higher-order transformer lazy accepts iterable returns Closure pure
(int|string → bool) → (Iterable → Iterable)
At a glance — Like filter but tests keys instead of values. Lazy — Generator in, Generator out, keys preserved.

Create a function which filters an arrays keys using the defined predicate.

/**
 * Returns a Closure for filtering the passed arrays keys
 *
 * @param callable(mixed): bool $callable The predicate function that determines if the value should be kept.
 * @return Closure(array<int|string, mixed>): array<int|string, mixed>
 */
Arrays\filterKey(callable $callback): Closure

Returned Closure

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

/**
  * @param array<int|string, mixed> $array
  * @return array<int|string, mixed>
  */
$function (array $data): array

Examples

Partial Application

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

// Create a function that will filter out all values that are not strings.
$filter = Arrays\filterKey(fn($v) => $v % 2 === 0);  

// Called as a function.
var_dump($filterKey([1 => 'a', 2 => 'b' ])); // ['b']

Curried

This can be called inline using currying.

// Filter an array for all even numbers.
var_dump(Arrays\filterKey(fn($v) => $v % 2 === 0)([1 => 'a', 2 => 'b' ])); // ['b']

Details

Arrays Functions

Releated Array filter Functions