无法从多维数组PHP中删除空值

时间:2013-07-04 13:49:32

标签: php

我的多维数组如下所示:

Array
(
[0] => Array
    (
        [0] => 2010-12-03
        [1] => 0
        [2] => Array
            (
                [0] => Array
                    (
                        [0] => 
                        [1] => 
                        [2] => 0
                        [3] => 
                        [4] => 
                    )

                [1] => Array
                    (
                        [0] => 
                        [1] => 
                        [2] => 0
                        [3] => 
                        [4] => 
                    )

                [2] => Array
                    (
                        [0] => 
                        [1] => 
                        [2] => 0
                        [3] => 
                        [4] => 
                    )

                [3] => Array
                    (
                        [0] => 
                        [1] => 
                        [2] => 0
                        [3] => 
                        [4] => 
                    )

                [4] => Array
                    (
                        [0] => 
                        [1] => 
                        [2] => 0
                        [3] => 
                        [4] => 
                    )

                [5] => Array
                    (
                        [0] => 
                        [1] => 
                        [2] => 0
                        [3] => 
                        [4] => 
                    )

            )

    )

[1] => Array
    (
        [0] => 2010-12-10
        [1] => 486
        [2] => Array
            (
                [0] => Array
                    (
                        [0] => Bob
                        [1] => Lucy
                        [2] => 54
                        [3] => Y
                        [4] => PC1Clean
                    )

                [1] => Array
                    (
                        [0] => Jo
                        [1] => Mary
                        [2] => 432
                        [3] => Y
                        [4] => PC2Bar
                    )

                [2] => Array
                    (
                        [0] => 
                        [1] => 
                        [2] => 0
                        [3] => 
                        [4] => 
                    )

                [3] => Array
                    (
                        [0] => 
                        [1] => 
                        [2] => 0
                        [3] => 
                        [4] => 
                    )

                [4] => Array
                    (
                        [0] => 
                        [1] => 
                        [2] => 0
                        [3] => 
                        [4] => 
                    )

                [5] => Array
                    (
                        [0] => 
                        [1] => 
                        [2] => 0
                        [3] => 
                        [4] => 
                    )

            )

    )

我尝试了array_filter和不同的循环迭代来删除zero / null值,例如

function removeElementWithValue($array, $key, $value) {
    foreach($array as $subKey => $subArray) {
        if($subArray[$key] == $value) {
            unset($array[$subKey]);
        }
    }
}

但似乎没有任何效果。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:3)

您需要递归调用您的删除功能。 (为数组中的每个子值调用函数,然后函数将自动为每个子子值等调用自身,直到它无法在树中继续进行 )。

这是一个快速制作(未经测试)的函数,它应该删除所有空值(包括数组,如果它们是空的):

function removeEmptyElements($array) {
    foreach ($array as $key => $value){
        if (empty($value)) {
            unset($array[$key]);
        } else if (is_array($value)) {
            $array[$key] = removeEmptyElements($value);
        }
    }
    return $array;
}

答案 1 :(得分:0)

也许这个答案有点太晚了,但现在是:

$array = array( // array with examples
    array('123',' 456', array(123, null)),
    array('', null, '123'),
    123,
    234,
    0,
    null,
); 

$filterEmptyValues = function($val) use (&$filterEmptyValues) {
    foreach($val as $key => $value) {
        if (!is_array($value)) {
            if (!$value) unset($val[$key]);
        }
        else $val[$key] = call_user_func($filterEmptyValues, $value);
    }
    return $val;
};


$array = $filterEmptyValues($array) ;

print_r($array);