简体   繁体   English

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

[英]Can't remove empty values from multi-dimensional array PHP

My multi-dimensional array looks like this: 我的多维数组如下所示:

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] => 
                    )

            )

    )

I've tried array_filter and different loop iterations to remove the zero / null values, such as 我尝试过array_filter和不同的循环迭代来删除zero / null值,例如

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

But nothing seems to be working. 但是似乎没有任何作用。 Any help would be much appreciated! 任何帮助将非常感激!

You need to recursively call your remove function. 您需要递归调用remove函数。 ( Call the function for every sub-value in the array, and then the function will call itself for every sub-sub-value etc. automatically until it can't get any further down the tree ). 为数组中的每个子值调用该函数,然后该函数将自动为每个子子值等调用自身,直到无法在树上深入为止 )。

Here's a quickly made (untested) function that should remove all empty values (including arrays, if they're empty): 这是一个快速制作(未经测试)的函数,该函数应删除所有空值(包括数组,如果它们为空):

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

Maybe this answer is a bit too late, but here it is: 也许这个答案为时已晚,但是这里是:

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM