Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
Filter recursive array and only remove NULL value

I want to remove all null or blank value but not false and 0 value from recursive array.

function isNotNull($val) {
    if(is_array($val)) {
        $ret = array_filter($val, 'isNotNull');
        return $ret;
    } else {
        return (!is_null($val) && $val !== '');
   }

}

$arr = array_filter($arr, 'isNotNull');

Input:

$arr = array(
"stringKey" => "Abc",
"boolKey" => false,
"zeroKey" => 0,
"blankKey" => '',
"newArr" => array(
    "stringKey2"=>"Abc2", 
    "boolKey2"=>false, 
    "zeroKey2" => 0, 
    "blankKey2"=>"", 
    "blankArr" => array()
    )
);

This give output:

Array
(
    [stringKey] => Abc
    [boolKey] => 
    [zeroKey] => 0
    [newArr] => Array
        (
            [stringKey2] => Abc2
            [boolKey2] => 
            [zeroKey2] => 0
            [blankKey2] => 
            [blankArr] => Array
                (
                )
        )
)

But i want to bellow output:

 Array
(
    [stringKey] => Abc
    [boolKey] => 
    [zeroKey] => 0
    [newArr] => Array
        (
            [stringKey2] => Abc2
            [boolKey2] => 
            [zeroKey2] => 0
        )
)

I used array_filter with callback function but it only filter simple array not multidimensional array. I don't want to use loop.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You could combine array_map and array_filter in an recursive called function. Something like this could work for you.

function filterNotNull($array) {
    $array = array_map(function($item) {
        return is_array($item) ? filterNotNull($item) : $item;
    }, $array);
    return array_filter($array, function($item) {
        return $item !== "" && $item !== null && (!is_array($item) || count($item) > 0);
    });
}
about 4 years ago · Santiago Trujillo Report

0

Don't need to reinvent recursion yourself. You can use RecursiveCallbackFilterIterator:

$iterator = new RecursiveIteratorIterator(
    new RecursiveCallbackFilterIterator(
        new RecursiveArrayIterator($arr),
        function ($value) {
            return $value !== null && $value !== '';
        }
    )
);

$result = iterator_to_array($iterator);

Here is working demo.

You should try to use as much stuff from Standard PHP Library (SPL) as you can.

UPDATE: As it is stated in the comments, the solution with iterator not actually suits for this purpose.

In the comments to the array_walk_recursive function you can find the implementation of walk_recursive_remove function:

function walk_recursive_remove (array $array, callable $callback) { 
    foreach ($array as $k => $v) { 
        if (is_array($v)) { 
            $array[$k] = walk_recursive_remove($v, $callback); 
        } else { 
            if ($callback($v, $k)) { 
                unset($array[$k]); 
            } 
        } 
    } 

    return $array; 
} 

This generalized version of recursion function takes criteria in the form of callback. Having this function you can remove empty elements like this:

$result = walk_recursive_remove($arr, function ($value) {
    return $value === null || $value === '';
});

Here is working demo.

about 4 years ago · Santiago Trujillo Report

0

Here you require filtering of an array for empty string. In this code below you can add no of checks to filter it recursively. Hope it will work fine.

Try this code snippet here

<?php

ini_set('display_errors', 1);
//Using more complexed sample array for filter it.
$arr = array(
    "stringKey" => "Abc",
    "boolKey" => false,
    "zeroKey" => 0,
    "blankKey" => '',
    "newArr" => array(
        "stringKey2" => "Abc2",
        "boolKey2" => false,
        "zeroKey2" => 0,
        "blankKey2" => "",
        "blankArr" => array(
            "blankString"=>"",
            "zeroKey"=>0,
            "blankArr3"=>array()
        )
    )
);

$result=recursiveFilter($arr);
print_r($result);


function recursiveFilter($value)
{
    foreach ($value as $key => $value1)
    {
        if ($value1 === "")  unset($value[$key]);//you can add no. of conditions here.
        else if (is_array($value1)) $value[$key] = recursiveFilter($value1);
    }
    return $value;
}

Output:

Array
(
    [stringKey] => Abc
    [boolKey] => 
    [zeroKey] => 0
    [newArr] => Array
        (
            [stringKey2] => Abc2
            [boolKey2] => 
            [zeroKey2] => 0
            [blankArr] => Array
                (
                    [zeroKey] => 0
                    [blankArr3] => Array
                        (
                        )

                )

        )

)
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!