How to ignore or remove null element from an array inside collection

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Ignore or Remove Null Elements from an Array Inside a Collection

As developers working with data structures like PHP arrays and Laravel Collections, we frequently encounter scenarios where data is incomplete, resulting in null entries within a list. Dealing with these nulls efficiently is crucial for maintaining clean, predictable code. When you have a collection of items, and some of those items are missing or explicitly set to null, blindly processing them can lead to runtime errors or incorrect logic.

The challenge presented here is: given a collection item like this:

Collection {#363 ▼
  #items: array:3 [▼
    0 => Tag {#376 ▶}
    1 => null
    2 => Tag {#374 ▶}
  ]
}

We want to remove the element at index 1 (the null) and consolidate the remaining elements.

This post will explore the most effective, idiomatic ways to filter out null values from an array within a collection, focusing on clarity and performance.

The Best Approach: Using array_filter()

The most straightforward and highly recommended method in PHP for removing specific values (like null) from an array is the built-in function array_filter(). This function iterates over the array and only keeps the elements for which a callback function returns true. Since null evaluates to false, we can use this property directly.

When working with Laravel Collections, you can apply this method directly to the underlying array of items.

Code Example: Filtering Nulls

Let's assume we have the collection instance and want to clean up its items:

use Illuminate\Support\Collection;

// Simulate the initial collection structure
$collection = new Collection([
    new class { public string $name = 'Tag A'; }, // Item 0
    null,                                    // Item 1 (The null we want to remove)
    new class { public string $name = 'Tag B'; }  // Item 2
]);

// Access the underlying array for manipulation
$items = $collection->toArray();

// Use array_filter to remove all null values
$filteredItems = array_filter($items, function ($item) {
    // This condition ensures that only non-null items are kept.
    return $item !== null;
});

// Reassign the filtered results back to the collection (optional, depending on context)
$collection->setItems(array_values($filteredItems));

// Output the result
print_r($collection->items);

Explanation:

  1. We extract the items into a standard PHP array (toArray()).
  2. array_filter() iterates through this array. The anonymous function checks if the current $item is not strictly equal to null.
  3. The resulting $filteredItems array will contain only the elements that passed the filter (Tag A and Tag B).

This approach is highly readable, efficient, and leverages core PHP functionality, making it a solid practice for data sanitation within your application logic.

Alternative Method: Using array_values() with Filtering

Another useful technique involves chaining operations to ensure the resulting array is properly re-indexed, which can be helpful if you are dealing with complex nested structures or need guaranteed sequential keys. While array_filter() handles removal perfectly, combining it with functions like array_values() ensures the final collection maintains a clean, zero-indexed sequence.

For instance, after filtering, using array_values() guarantees that the resulting array has contiguous integer keys starting from 0, which is ideal for collections. This concept of manipulating arrays to fit framework expectations is central to how Laravel manages data; for example, understanding collection methods often starts with mastering underlying PHP array manipulation.

Conclusion: Clean Data, Cleaner Code

Dealing with null values in arrays within a collection is an everyday task for any backend developer. By prioritizing standard, efficient functions like array_filter(), you ensure your code remains robust, readable, and free of potential errors caused by unexpected data states. Always aim to filter and sanitize your data at the point of entry or processing to prevent these types of issues from propagating deeper into your application logic. Mastering these array manipulation techniques is key to writing high-quality, maintainable code, much like understanding how Eloquent models interact with their underlying database structures on platforms like laravelcompany.com.