Skip to content

Commit

Permalink
[8.x] Add method filterNulls() to Arr (#39921)
Browse files Browse the repository at this point in the history
* Update Arr.php

filterNulls

* Update SupportArrTest.php

* Update SupportArrTest.php

* StyleCI

* formatting - rename mehtod

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
chu121su12 and taylorotwell committed Dec 7, 2021
1 parent 8c1f6ce commit c16367a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
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

0 comments on commit c16367a

Please sign in to comment.