diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index 1ce093079f64..702bde2524d5 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -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. * diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index e34f469c848d..5d405b1f9332 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -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];