Skip to content

Commit c16367a

Browse files
[8.x] Add method filterNulls() to Arr (#39921)
* Update Arr.php filterNulls * Update SupportArrTest.php * Update SupportArrTest.php * StyleCI * formatting - rename mehtod Co-authored-by: Taylor Otwell <[email protected]>
1 parent 8c1f6ce commit c16367a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/Illuminate/Collections/Arr.php

+11
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,17 @@ public static function where($array, callable $callback)
717717
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
718718
}
719719

720+
/**
721+
* Filter items where the value is not null.
722+
*
723+
* @param array $array
724+
* @return array
725+
*/
726+
public static function whereNotNull($array)
727+
{
728+
return static::where($array, fn ($x) => ! is_null($x));
729+
}
730+
720731
/**
721732
* If the given value is not an array and not null, wrap it in one.
722733
*

tests/Support/SupportArrTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ public function testExists()
163163
$this->assertFalse(Arr::exists(new Collection(['a' => null]), 'b'));
164164
}
165165

166+
public function testWhereNotNull()
167+
{
168+
$array = array_values(Arr::whereNotNull([null, 0, false, '', null, []]));
169+
$this->assertEquals([0, false, '', []], $array);
170+
}
171+
166172
public function testFirst()
167173
{
168174
$array = [100, 200, 300];

0 commit comments

Comments
 (0)