Hi internals,
I'd like to start the discussion on a new RFC:
https://wiki.php.net/rfc/array_partition
It proposes a function that splits an array into the elements for which
a callback returns a truthy value and those for which it does not, in a
single pass:
function array_partition(array $array, callable $callback): array {}
[$adults, $minors] = array_partition($users, fn($u) => $u->age >= 18);
Keys are preserved, and the callback receives the value and the key,
following array_find(). The result is always a list of two arrays, with
the matching elements first, as in Ruby, Kotlin, Rust, Haskell and
Laravel's Collection::partition().
The RFC also covers the points raised in the karma request thread:
why it is a two-way split rather than a general grouping function, why
the result is not keyed by true/false, and the $callback parameter
name. It also discusses the previous userland uses of the name
array_partition() with an "N chunks" meaning.
The implementation is at https://github.com/php/php-src/pull/24568.
Thanks to Ilija for granting the karma, and to everyone who commented
on the earlier thread. I look forward to your feedback.
Ayoub