Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

93
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda