Skip to content

[12.x] Feature: Array partition without preserving keys #54942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -950,18 +950,24 @@ public static function reject($array, callable $callback)
*
* @param iterable<TKey, TValue> $array
* @param callable(TValue, TKey): bool $callback
* @return array<int<0, 1>, array<TKey, TValue>>
* @param bool $preserveKeys
* @return ($preserveKeys is true ? array<int<0, 1>, array<TKey, TValue>> : array<int<0, 1>, list<TValue>>)
*/
public static function partition($array, callable $callback)
public static function partition($array, callable $callback, bool $preserveKeys = true)
{
$passed = [];
$failed = [];

$add = match ($preserveKeys) {
true => fn (array &$array, mixed $value, int|string $key) => $array[$key] = $value,
false => fn (array &$array, mixed $value, int|string $key) => $array[] = $value,
};

foreach ($array as $key => $item) {
if ($callback($item, $key)) {
$passed[$key] = $item;
$add($passed, $item, $key);
} else {
$failed[$key] = $item;
$add($failed, $item, $key);
}
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1565,9 +1565,11 @@ public function testReject()
public function testPartition()
{
$array = ['John', 'Jane', 'Greg'];

$result = Arr::partition($array, fn (string $value) => str_contains($value, 'J'));

$this->assertEquals([[0 => 'John', 1 => 'Jane'], [2 => 'Greg']], $result);

$assocArray = ['John', 'Jane', 'Greg'];
$result = Arr::partition($assocArray, fn (string $value) => str_contains($value, 'J'), false);
$this->assertEquals([[0 => 'John', 1 => 'Jane'], [0 => 'Greg']], $result);
}
}
6 changes: 6 additions & 0 deletions types/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use function PHPStan\Testing\assertType;

/** @var array<int, User> $array */
$array = [new User];
/** @var iterable<int, User> $iterable */
$iterable = [];
/** @var Traversable<int, User> $traversable */
$traversable = new ArrayIterator([new User]);
/** @var array<string, User> $associativeArray */
$associativeArray = ['John' => new User];

assertType('User|null', Arr::first($array));
assertType('User|null', Arr::first($array, function ($user) {
Expand Down Expand Up @@ -99,3 +102,6 @@
assertType("'string'|User", Arr::last($traversable, null, function () {
return 'string';
}));

assertType('array<int<0, 1>, array<string, User>>', Arr::partition($associativeArray, fn ($user) => true));
assertType('array<int<0, 1>, list<User>>', Arr::partition($associativeArray, fn ($user) => true, false));
Loading