[php-src] PR #24568: RFC: array_partition()
| From: | ayoubezzini | Date: | Thu, 22 Oct 2026 18:36:42 +0000 |
| Subject: | [php-src] PR #24568: RFC: array_partition() | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-39732@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/24568
Author: ayoubezzini
Adds
array_partition(), which splits an array into the elements that satisfy a callback
and those that don't, in a single pass.
RFC: https://wiki.php.net/rfc/array_partition
(TBD)
## Spec
```php
function array_partition(array $array, callable $callback, int $mode = ARRAY_FILTER_USE_VALUE):
array
```
- Returns [$matching, $nonMatching].
- Keys are preserved, like array_filter().
- $mode accepts ARRAY_FILTER_USE_VALUE (0),
ARRAY_FILTER_USE_KEY and ARRAY_FILTER_USE_BOTH, with the same semantics as
array_filter(). Any other value throws a ValueError.
- The callback's return value is evaluated with standard truthiness, like
array_filter().
- Exceptions thrown by the callback propagate, and partial results are discarded.
## Example
```php
[$adults, $minors] = array_partition($users, fn($u) => $u->age >= 18);
```
Today this takes either two array_filter() passes with a negated callback, or
array_filter() followed by array_diff_key().
## Implementation notes
- The mode validation, callback invocation and element insertion of array_filter() were
extracted into shared static helpers, so both functions behave identically.
array_filter() performance is unchanged (within noise on a 1M-element benchmark).
- Tests cover basic splitting, empty input, all/none matching, key preservation (int and string
keys), ARRAY_FILTER_USE_KEY/ARRAY_FILTER_USE_BOTH, invalid modes,
exceptions in the callback, and by-reference elements.