0

我的多维数组如下所示:

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]);
        }
    }
}

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

4

2 回答 2

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;
}
于 2013-07-04T13:53:08.507 回答
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);
于 2013-07-04T14:18:14.103 回答