Arr::whereNotNull

Removes every null value from an array while leaving other falsy values in place.

Signature
public static function whereNotNull($array)

Parameters

ParameterTypeDescription
$$arrayarrayThe array to filter. Only top-level values are inspected.

Return value

arrayA new array with all null values removed and the original keys preserved.

Arr::whereNotNull is Arr::where($array, fn ($value) => ! is_null($value)). The precision is the point: it removes only nulls, not the other falsy values that a bare array_filter($array) would also throw away.

When to reach for it

  • Cleaning an update payload so absent fields are skipped while a deliberate 0 or false is still written.
  • Tidying the output of Arr::pluck, which emits a null for every row missing the requested path.
  • Preparing query parameters or a JSON body where null means "not provided" but 0 is a real value.

What trips people up

It is not array_filter. Reaching for array_filter($array) to "remove empties" also deletes 0, 0.0, '', '0', false and []. That is how a price of zero or a disabled boolean flag silently vanishes from an update.

Keys are preserved. As with Arr::where, the result can be a gappy array that json_encode renders as an object. Add array_values() when a list is required.

Only the top level is filtered. Nulls nested inside sub-arrays survive; recurse yourself if you need a deep clean.

The mirror method Arr::whereNotEmpty… does not exist — use Arr::where with your own emptiness test instead.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Strip unset fields from an update payload while preserving a deliberate zero.
  • Clean nulls out of an Arr::pluck result where some rows lacked the path.
  • Build query string parameters where null means omitted but 0 is meaningful.

Frequently asked questions

What is the difference between Arr::whereNotNull and array_filter?

Arr::whereNotNull removes only null, while a bare array_filter removes every falsy value including 0, '', false and empty arrays. Use whereNotNull whenever a zero or a false is a legitimate value.

Does Arr::whereNotNull reindex the array?

No, the original keys are preserved, so the result can have gaps. Wrap it in array_values() if you need a zero-indexed list.

Does it remove nulls from nested arrays?

No, only top-level values are checked. Write a recursive helper, or map Arr::whereNotNull over the sub-arrays yourself.

More examples

Building a partial update payload

Three keys survive, including the two whose values are falsy but intentional. Using array_filter here would have produced {"name":"Standing Desk"} and quietly refused to set the price to zero.

Example · php
<?php

use Illuminate\Support\Arr;

// null means "the client did not send this field".
$input = [
    'name' => 'Standing Desk',
    'price' => 0,          // deliberately free
    'featured' => false,   // deliberately off
    'description' => null, // not provided
    'sku' => null,         // not provided
];

$changes = Arr::whereNotNull($input);

print_r(array_keys($changes));

// $product->update($changes) writes price = 0 and featured = false,
// and leaves description and sku exactly as they were.

echo json_encode($changes), PHP_EOL;

/*
Array
(
    [0] => name
    [1] => price
    [2] => featured
)
{"name":"Standing Desk","price":0,"featured":false}
*/

Cleaning up after Arr::pluck

Pluck returns one entry per order, so orders 2 and 3 contribute nulls. whereNotNull drops both but leaves keys 0 and 3, which json_encode renders as an object — the array_values step is what turns it back into a JSON array.

Example · php
<?php

use Illuminate\Support\Arr;

$orders = [
    ['id' => 1, 'shipping' => ['tracking' => 'AB1']],
    ['id' => 2],
    ['id' => 3, 'shipping' => ['tracking' => null]],
    ['id' => 4, 'shipping' => ['tracking' => 'CD2']],
];

$raw = Arr::pluck($orders, 'shipping.tracking');
echo count($raw), PHP_EOL;

// Missing paths and explicit nulls are both removed; keys still have gaps.
$clean = Arr::whereNotNull($raw);
echo json_encode($clean), PHP_EOL;

// array_values() restores a list.
echo json_encode(array_values($clean)), PHP_EOL;

/*
4
{"0":"AB1","3":"CD2"}
["AB1","CD2"]
*/

Discussion

  • Be the first to comment on this lesson.