Arrays\filterAnd()

higher-order transformer variadic lazy accepts iterable returns Closure pure
...(T → bool) → (Iterable → Iterable)
At a glance — AND-combines multiple predicates into a single filter. Lazy — Generator in, Generator out, keys preserved.

Create a function which filters an array using multiple predicates, all of which must return true.

/**
 * Returns a Closure for filtering the passed array using multiple predicates, all of which must return true.
 *
 * @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\filterAnd(callable ...$callback): Closure

Returned Closure

When Arrays\filterAnd() 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 string that have 4 or more characters.
$filter = Arrays\filterAnd(
 'is_string',
 fn($v) => strlen($v) >= 4
);

// Called as a function.
var_dump($filter([1 => 'abc', 2 => 3.14, 3 => 'abcd' ])); // ['abcd']

Curried

This can be called inline using currying.

// Filter an array for all even numbers.

var_dump(Arrays\filterAnd('is_numeric', fn($v) => $v % 2 === 0)(['Apple', 2, 3, 4, False, 6])); 

// [2, 4, 6]

Details

Arrays Functions

Releated Array filter Functions