Laravel - Remove elements having NULL value from multidimentional array

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Laravel: Removing NULL Elements from Multidimensional Arrays As senior developers, we frequently deal with complex, nested data structures when working with APIs, database results, or serialized data. In the PHP ecosystem, especially within a framework like Laravel, dealing with multidimensional arrays that contain `NULL` values requires careful handling to ensure data integrity and prevent runtime errors. Today, we will tackle a common challenge: how to effectively remove all elements that hold the `NULL` value from deeply nested arrays. We will explore a robust, recursive approach tailored for this specific scenario. ## The Challenge with Nested NULLs Imagine you have retrieved complex relational data—perhaps through an Eloquent query in Laravel—and you find that some of the nested relationships or optional fields contain `NULL`. Removing these non-existent entries is crucial for sanitizing your data before further processing, sorting, or serialization. Consider the structure you provided: ```php Array ( [id] => 37141 [last_done_on] => [] [children] => Array ( [0] => NULL /* This must be removed */ [1] => Array(...) // ... other children ) ) ``` The goal is to recursively traverse this structure and eliminate any array element that evaluates to `NULL`. Standard flat filtering methods are insufficient for deeply nested data. ## The Recursive Solution: Deep Filtering Since the structure can be arbitrarily deep, the most reliable way to solve this is by implementing a recursive function. This function will check every element in an array. If the element is itself an array, it calls itself on that subarray; if it's a scalar value, it checks if it is `NULL` and decides whether to keep it or discard it. Here is a practical PHP implementation designed to handle multidimensional arrays recursively: ```php function removeNullRecursive(array $array): array { $result = []; foreach ($array as $key => $value) { // Check if the current value is NULL if ($value !== NULL) { // If the value is not NULL, process it. if (is_array($value)) { // If the value is an array, recurse into it $result[$key] = removeNullRecursive($value); } else { // Otherwise, keep the scalar value $result[$key] = $value; } } } return $result; } // --- Example Usage --- $multidimensionalArray = [ 'id' => 37141, 'last_done_on' => [], 'children' => [ 0 => NULL, // Element to be removed 1 => [ 'id' => 37142, 'last_done_on' => [], 'children' => [] ], 2 => [ 'id' => 37143, 'last_done_on' => [], 'children' => [ 0 => [ 'id' => 37144, 'last_done_on' => [], 'children' => [] ], 1 => [ 'id' => 37145, 'last_done_on' => [], 'children' => [] ] ] ], 3 => [ 'id' => 37157, 'last_done_on' => [], 'children' => [ 0 => [ 'id' => 37158, 'last_done_on' => [], 'children' => [] ], 1 => [ 'id' => 37159, 'last_done_on' => [], 'children' => [] // The NULL within this array is gone ] ] ] ] ]; $cleanedArray = removeNullRecursive($multidimensionalArray); // Output the result (formatted for readability) print_r($cleanedArray); ``` ### Explanation of the Approach The function `removeNullRecursive` iterates through every item in the input array. The core logic relies on three checks: 1. **`if ($value !== NULL)`**: This is the primary filter. If an element is strictly not `NULL`, we proceed. 2. **`if (is_array($value))`**: If the surviving value is itself an array, it means we have found a nested structure. Therefore, we call `removeNullRecursive()` on this subarray to ensure that any `NULL`s within it are also eliminated. This recursive step is what allows us to dive into the entire tree of data. 3. **Else**: If the value is not an array (it's a string, integer, etc.), we simply assign it to the result, as it has passed the `NULL` check. This method guarantees that every level of nesting is inspected and cleaned, yielding the perfectly filtered structure you require. ## Conclusion Handling complex data structures like multidimensional arrays efficiently is a hallmark of strong backend development. While Laravel provides powerful tools for database interaction (like Eloquent) and collection manipulation, understanding fundamental recursive PHP logic remains essential when dealing with raw array data. By employing a custom recursive function, as demonstrated above, you gain precise control over sanitizing deeply nested data, ensuring that your application operates on clean, reliable information. Mastering these foundational techniques will make you a more effective and senior developer.