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

[8.x] Add method filterNulls() to Arr #39921

Merged
merged 5 commits into from Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Illuminate/Collections/Arr.php
Expand Up @@ -717,6 +717,17 @@ public static function where($array, callable $callback)
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
}

/**
* Filter items where the value is not null.
*
* @param array $array
* @return array
*/
public static function whereNotNull($array)
{
return static::where($array, fn ($x) => ! is_null($x));
}

/**
* If the given value is not an array and not null, wrap it in one.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/Support/SupportArrTest.php
Expand Up @@ -163,6 +163,12 @@ public function testExists()
$this->assertFalse(Arr::exists(new Collection(['a' => null]), 'b'));
}

public function testWhereNotNull()
{
$array = array_values(Arr::whereNotNull([null, 0, false, '', null, []]));
$this->assertEquals([0, false, '', []], $array);
}

public function testFirst()
{
$array = [100, 200, 300];
Expand Down