Skip to content

[11.x] Feature: Array reject#54638

Merged
taylorotwell merged 4 commits into
laravel:11.xfrom
liamduckett:feature/array-reject
Feb 17, 2025
Merged

[11.x] Feature: Array reject#54638
taylorotwell merged 4 commits into
laravel:11.xfrom
liamduckett:feature/array-reject

Conversation

@liamduckett
Copy link
Copy Markdown
Contributor

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

This will allow users to simplify array filtering code like so:

// Declare array
$array = ['Liam Duckett', 'Liam Doe', 'John Doe', 'John Liam'];

// -- Previously --
$liams = Arr::where($array, fn($value) => str_contains($value, 'Liam'));
$notLiams = Arr::where($array, fn($value) => ! str_contains($value, 'Liam'));

// -- With this PR --
$liams = Arr::where($array, fn($value) => str_contains($value, 'Liam'));
$notLiams = Arr::reject($array, fn($value) => str_contains($value, 'Liam'));

This also brings it more aligned with Collections which already have a reject method. I also thought about adding an alias filter for the where method, but decided this was better done in another PR (thoughts welcome)?

@liamduckett liamduckett changed the title Feature/array reject [11.x] Feature: Array reject Feb 16, 2025
@taylorotwell taylorotwell merged commit 5e5bc04 into laravel:11.x Feb 17, 2025
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