从多维数组中删除空数组

时间:2014-11-12 08:55:43

标签: php arrays

我有以下数组

$cal = array(
    0 => array(
        0 => array(
            0 => '*',
            1 => 2,
            2 => 3,
        ) ,
        1 => array(
            0 => 6,
            1 => 7,
            2 => 8,
            3 => 9,
            4 => 10,
        ) ,
        2 => array(
            0 => '*',
            1 => '*',
            2 => 15,
            3 => '*',
            4 => 17,
        ) ,
        3 => array(
            0 => 20,
            1 => 21,
            2 => 22,
            3 => 23,
            4 => 24,
        ) ,
        4 => array(
            0 => 27,
            1 => 28,
            2 => 29,
            3 => 30,
            4 => 31,
        ) ,
    ) ,
    1 => array(
        0 => array() ,
        1 => array(
            0 => 3,
            1 => 4,
            2 => 5,
            3 => '*',
            4 => 7,
        ) ,
        2 => array(
            0 => 10,
            1 => 11,
            2 => 12,
            3 => 13,
            4 => 14,
        ) ,
        3 => array(
            0 => 17,
            1 => 18,
            2 => 19,
            3 => 20,
            4 => 21,
        ) ,
        4 => array(
            0 => 24,
            1 => 25,
            2 => 26,
            3 => 27,
            4 => 28,
        ) ,
    ) ,
    2 => array(
        0 => array() ,
        1 => array(
            0 => 3,
            1 => 4,
            2 => 5,
            3 => 6,
            4 => '*',
        ) ,
        2 => array(
            0 => 10,
            1 => 11,
            2 => 12,
            3 => 13,
            4 => 14,
        ) ,
        3 => array(
            0 => 17,
            1 => 18,
            2 => 19,
            3 => 20,
            4 => 21,
        ) ,
        4 => array(
            0 => 24,
            1 => 25,
            2 => 26,
            3 => 27,
            4 => 28,
        ) ,
        5 => array(
            0 => 31,
        ) ,
    ) ,
    3 => array(
        0 => array(
            0 => 1,
            1 => 2,
            2 => 3,
            3 => 4,
        ) ,
        1 => array(
            0 => 7,
            1 => 8,
            2 => 9,
            3 => 10,
            4 => 11,
        ) ,
        2 => array(
            0 => 14,
            1 => 15,
            2 => 16,
            3 => 17,
            4 => 18,
        ) ,
        3 => array(
            0 => 21,
            1 => 22,
            2 => 23,
            3 => 24,
            4 => 25,
        ) ,
        4 => array(
            0 => 28,
            1 => 29,
            2 => 30,
        ) ,
    ) ,
    4 => array(
        0 => array(
            0 => 1,
            1 => 2,
        ) ,
        1 => array(
            0 => '*',
            1 => '*',
            2 => '*',
            3 => 8,
            4 => 9,
        ) ,
        2 => array(
            0 => 12,
            1 => 13,
            2 => 14,
            3 => 15,
            4 => 16,
        ) ,
        3 => array(
            0 => 19,
            1 => 20,
            2 => 21,
            3 => 22,
            4 => 23,
        ) ,
        4 => array(
            0 => 26,
            1 => 27,
            2 => 28,
            3 => 29,
            4 => 30,
        ) ,
    ) ,
    5 => array(
        0 => array() ,
        1 => array(
            0 => 3,
            1 => '*',
            2 => 5,
            3 => 6,
        ) ,
        2 => array(
            0 => 9,
            1 => 10,
            2 => 11,
            3 => 12,
            4 => '*',
        ) ,
        3 => array(
            0 => 16,
            1 => 17,
            2 => 18,
            3 => 19,
            4 => 20,
        ) ,
        4 => array(
            0 => 23,
            1 => 24,
            2 => 25,
            3 => 26,
            4 => 27,
        ) ,
        5 => array(
            0 => 30,
        ) ,
    ) ,
    6 => array(
        0 => array(
            0 => 1,
            1 => 2,
            2 => 3,
            3 => 4,
        ) ,
        1 => array(
            0 => 7,
            1 => 8,
            2 => 9,
            3 => 10,
            4 => 11,
        ) ,
        2 => array(
            0 => 14,
            1 => 15,
            2 => 16,
            3 => 17,
            4 => 18,
        ) ,
        3 => array(
            0 => 21,
            1 => 22,
            2 => 23,
            3 => 24,
            4 => 25,
        ) ,
        4 => array(
            0 => 28,
            1 => 29,
            2 => 30,
            3 => 31,
        ) ,
    ) ,
    7 => array(
        0 => array(
            0 => 1,
        ) ,
        1 => array(
            0 => 4,
            1 => 5,
            2 => 6,
            3 => 7,
            4 => 8,
        ) ,
        2 => array(
            0 => 11,
            1 => 12,
            2 => 13,
            3 => 14,
            4 => 15,
        ) ,
        3 => array(
            0 => 18,
            1 => 19,
            2 => 20,
            3 => 21,
            4 => 22,
        ) ,
        4 => array(
            0 => 25,
            1 => 26,
            2 => 27,
            3 => 28,
            4 => 29,
        ) ,
        5 => array() ,
    ) ,
    8 => array(
        0 => array(
            0 => 1,
            1 => 2,
            2 => 3,
            3 => 4,
            4 => 5,
        ) ,
        1 => array(
            0 => 8,
            1 => 9,
            2 => 10,
            3 => 11,
            4 => 12,
        ) ,
        2 => array(
            0 => 15,
            1 => 16,
            2 => 17,
            3 => 18,
            4 => 19,
        ) ,
        3 => array(
            0 => 22,
            1 => 23,
            2 => 24,
            3 => 25,
            4 => 26,
        ) ,
        4 => array(
            0 => 29,
            1 => 30,
        ) ,
    ) ,
    9 => array(
        0 => array(
            0 => 1,
            1 => 2,
            2 => 3,
        ) ,
        1 => array(
            0 => '*',
            1 => '*',
            2 => '*',
            3 => '*',
            4 => '*',
        ) ,
        2 => array(
            0 => '*',
            1 => '*',
            2 => '*',
            3 => '*',
            4 => '*',
        ) ,
        3 => array(
            0 => 20,
            1 => 21,
            2 => 22,
            3 => 23,
            4 => 24,
        ) ,
        4 => array(
            0 => 27,
            1 => 28,
            2 => 29,
            3 => 30,
            4 => 31,
        ) ,
    ) ,
    10 => array(
        0 => array() ,
        1 => array(
            0 => 3,
            1 => '*',
            2 => 5,
            3 => 6,
            4 => 7,
        ) ,
        2 => array(
            0 => 10,
            1 => 11,
            2 => 12,
            3 => 13,
            4 => 14,
        ) ,
        3 => array(
            0 => 17,
            1 => 18,
            2 => 19,
            3 => 20,
            4 => 21,
        ) ,
        4 => array(
            0 => 24,
            1 => 25,
            2 => 26,
            3 => '*',
            4 => 28,
        ) ,
        5 => array() ,
    ) ,
    11 => array(
        0 => array(
            0 => '*',
            1 => 2,
            2 => 3,
            3 => 4,
            4 => '*',
        ) ,
        1 => array(
            0 => 8,
            1 => 9,
            2 => 10,
            3 => 11,
            4 => 12,
        ) ,
        2 => array(
            0 => 15,
            1 => 16,
            2 => 17,
            3 => 18,
            4 => 19,
        ) ,
        3 => array(
            0 => 22,
            1 => 23,
            2 => 24,
            3 => 25,
            4 => 26,
        ) ,
        4 => array(
            0 => 29,
            1 => 30,
            2 => 31,
        ) ,
    ) ,
)

我尝试使用以下代码从其父级中删除空数组:

for($m = 0; $m < count($cal); $m++){
    for($w = 0; $w < count($cal[$m]); $w++){
        if(empty($cal[$m][$w])){
            array_shift($cal[$m]);
        }
    }
}

结果是(请参阅索引10)

10 => array(
        0 => array(
            0 => 10,
            1 => 11,
            2 => 12,
            3 => 13,
            4 => 14,
        ) ,
        1 => array(
            0 => 17,
            1 => 18,
            2 => 19,
            3 => 20,
            4 => 21,
        ) ,
        2 => array(
            0 => 24,
            1 => 25,
            2 => 26,
            3 => '*',
            4 => 28,
        ) ,
        3 => array() ,
    ) ,

我期望删除第一个和最后一个,而是删除第一个和第二个数组,并将最后一个数组保留在那里。任何想法?

4 个答案:

答案 0 :(得分:2)

尝试使用array_filter(),因为array_shift()只关注第一个元素:

foreach ($cal as &$value) {
    $value = array_filter($value);
}

答案 1 :(得分:0)

使用未设置($ cal [$ m] [$ w])而不是array_shift($ cal [$ m])。

这将导致

10 => array(
    1 => array(
        0 => 3,
        1 => '*',
        2 => 5,
        3 => 6,
        4 => 7,
    ) ,
    2 => array(
        0 => 10,
        1 => 11,
        2 => 12,
        3 => 13,
        4 => 14,
    ) ,
    3 => array(
        0 => 17,
        1 => 18,
        2 => 19,
        3 => 20,
        4 => 21,
    ) ,
    4 => array(
        0 => 24,
        1 => 25,
        2 => 26,
        3 => '*',
        4 => 28,
    ) ,
) ,

注意键[0]不再存在

答案 2 :(得分:0)

你可以使用一个简单的递归函数来遍历每个:

<?php

    function removeEmpties(&$array)
    {
        foreach($array as $key=>$val)
        {
            if(count($val)==0)
            {
                unset($array[$key]);
            }
            else
            {
                removeEmpties($val);
            }
        }
    }

    $yourArray=array( ... etc );
    removeEmpties($yourArray);

?>

答案 3 :(得分:0)

试试这段代码:

foreach ($cal as &$arr)
{
  $arr = array_filter($arr); 
}

$cal = array_filter($cal);