Creates a Closure that splits an array or iterable into two buckets by a predicate. Values where the callable returns truthy go in bucket 1, falsy values go in bucket 0.
/**
* @param callable(mixed):(bool|int) $function
* @return Closure(iterable<int|string, mixed>):array{0:mixed[], 1:mixed[]}
*/
Arrays\partition(callable $function): ClosureWhen Arrays\partition() is called, it returns the following Closure which can be used like a regular function.
/**
* @param iterable<int|string, mixed> $source
* @return array{0: mixed[], 1: mixed[]}
*/
$function (iterable $source): arrayThis can be used to create a simple closure which can be used as a regular function.
$byEven = Arrays\partition(fn($v) => $v % 2 === 0);
[$odd, $even] = $byEven([1, 2, 3, 4, 5, 6]);
print_r($odd); // [1, 3, 5]
print_r($even); // [2, 4, 6]This can be called inline using currying.
print_r(Arrays\partition('is_string')([1, 'a', 2, 'b']));
// [0 => [1, 2], 1 => ['a', 'b']]