Skip to content
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

[12.x] Feature: Array partition #54859

Merged
merged 5 commits into from
Mar 4, 2025

Conversation

liamduckett
Copy link
Contributor

Hey, this PR adds a partition method to the Array helper.

This allows using the existing partition logic, without needing to reach for collections.

// Pre code:
$numbers = [0, 1, 2, 3, 4, 5];

// Before:
[$evens, $odds] = collect($numbers)->partition(fn(int $number) => $number % 2 === 0);

// After:
[$evens, $odds] = Arr::partition($numbers, fn(int $number) => $number % 2 === 0);

I also converted the existing Collection::partition to use the Arr::partition method, to prevent duplication. Happy to revert this bit if preferable.

@crynobone crynobone changed the title Feature: Array partition [12.x] Feature: Array partition Mar 4, 2025
$failed = [];

foreach ($array as $key => $item) {
if ($callback($item, $key)) {
Copy link
Contributor

@pandiselvamm pandiselvamm Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

foreach ($array as $key => $item) {
        ($callback($item, $key) ? $passed : $failed)[$key] = $item;
}

@taylorotwell taylorotwell merged commit 6468835 into laravel:12.x Mar 4, 2025
41 checks passed
@taylorotwell
Copy link
Member

@liamduckett do you mind sending some docs over for this?

@liamduckett
Copy link
Contributor Author

Thanks @pandiselvamm. Speedy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants