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 without preserving keys #54942

Conversation

liamduckett
Copy link
Contributor

Hey, this PR allows using the existing Arr::partition method, without preserving keys.

I haven't added this to the EnumeratesValue trait (which Collection and LazyCollection make use of), due to it's additional complexity of operatorForWhere. This is not a problem (despite it's reliance on Arr::partition due to the new parameter being optional.

@taylorotwell
Copy link
Member

You can just call values on the results.

@liamduckett
Copy link
Contributor Author

@taylorotwell this feels very similar to #54916.

Calling array_values on the results feels a little verbose to me.

$array = ['John', 'Jane', 'Greg'];
$results = Arr::partition($array, fn (string $value) => str_contains($value, 'J'));

// Currently
$results = array_map(
    fn(array $result) => array_values($result),
    $results
);

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.

2 participants