PHP在更大,更复杂的数组中删除包含空值的数组

时间:2014-06-10 23:21:57

标签: php arrays unset array-filter

我知道这对于其他问题似乎是重复的,但我一直在测试其他问题的解决方案,而我无法让它们在我的特定情况下工作。

我有一个"复杂"名为$get_food的多维数组,如下所示:

Array (
    [0] => Array (
        [0] => Array (
            [dietID] => 562
            [blockNum] => 1
        )
        [1] => Array (
            [0] => Array (
                [0] => Array (
                    [mealTitle] =>
                )
                [1] => Array (
                    [foodNumber] => 1
                    [portions] => 1
                    [foodID] =>
                )
            )
            [1] => Array (
                [0] => Array (
                    [mealTitle] =>
                )
                [1] => Array (
                    [foodNumber] => 1
                    [portions] => 1
                    [foodID] =>
                )
            )
        )
    )
)
Array (
    [0] => Array (
        [0] => Array (
            [dietID] => 562
            [blockNum] => 2
        )
        [1] => Array (
            [0] => Array (
                [0] => Array (
                    [mealTitle] => Carnitas
                )
                [1] => Array (
                    [foodNumber] => 1
                    [portions] => 1
                    [foodID] =>
                )
            )
            [1] => Array (
                [0] => Array (
                    [mealTitle] => Geometry
                )
                [1] => Array (
                    [foodNumber] => 1
                    [portions] => 1
                    [foodID] => 21
                )
            )
        )
    )
)
Array (
    [0] => Array (
        [0] => Array (
            [dietID] => 562
            [blockNum] => 3
        )
        [1] => Array (
            [0] => Array (
                [0] => Array (
                    [mealTitle] => Carburation
                )
                [1] => Array (
                    [foodNumber] => 1
                    [portions] => 1
                    [foodID] =>
                )
                [2] => Array (
                    [foodNumber] => 2
                    [portions] => 1
                    [foodID] =>
                )
                [3] => Array (
                    [foodNumber] => 3
                    [portions] => 1
                    [foodID] =>
                )
                [4] => Array (
                    [foodNumber] => 4
                    [portions] => 1
                    [foodID] =>
                )
                [5] => Array (
                    [foodNumber] => 5
                    [portions] => 1
                    [foodID] =>
                )
            )
            [1] => Array (
                [0] => Array (
                    [mealTitle] => Bar Rescue
                )
                [1] => Array (
                    [foodNumber] => 1
                    [portions] => 1
                    [foodID] =>
                )
            )
        )
    )
)

我需要通过它迭代并删除空[foodID]的子数组。

e.g。使用键[5]删除整个子数组,因为它有一个空的[foodID]项,并且它没那么用:

[5] => Array (
    [foodNumber] => 5
    [portions] => 1
    [foodID] =>
)

一旦这些子数组被删除,我需要重建主数组,使其看起来与原来相同(具有相同的级别),但没有不需要的子数组。

我设法在更简单的多维数组上执行此操作,但这次不是。我不能让它在最后看起来一样。

这是我现在所遇到的令人沮丧的混乱代码,只是输出废话:

foreach($get_food as $key => $meal):    
    foreach($meal as $key1 => $mealpart){
        foreach($mealpart as $key2 => $food){
            //print_r($food);

            foreach($food as $key3 => $fooditem){
                if(($key3 != 0)&&($get_food[$key][$key1][$key2][$key3]['foodID'] == '')){

                    unset($get_food[$key][$key1][$key2][$key3]);

                    //echo 'No Food ID';
                } else {
                    $updatedFood[$key][$key1][$key2][$key3]= $fooditem;
                }   
            }

        }

    }       endforeach;

所以,非常感谢任何帮助:)

////////编辑

主阵列由8个阵列组成。 这里将var_export作为一个整合在一起:

$get_food = array(
0 => array(
    0 => array(
        0 => array(
            'dietID' => '562',
            'blockNum' => '1',
        ) ,
        1 => array(
            0 => array(
                0 => array(
                    'mealTitle' => '',
                ) ,
                1 => array(
                    'foodNumber' => '1',
                    'portions' => '1',
                    'foodID' => '33',
                ) ,
            ) ,
            1 => array(
                0 => array(
                    'mealTitle' => '',
                ) ,
                1 => array(
                    'foodNumber' => '1',
                    'portions' => '1',
                    'foodID' => '30',
                ) ,
            ) ,
            2 => array(
                0 => array(
                    'mealTitle' => '',
                ) ,
                1 => array(
                    'foodNumber' => '1',
                    'portions' => '1',
                    'foodID' => '34',
                ) ,
            ) ,
        ) ,
    ) ,
) ,
1 => array(
    0 => array(
        0 => array(
            'dietID' => '562',
            'blockNum' => '2',
        ) ,
        1 => array(
            0 => array(
                0 => array(
                    'mealTitle' => '',
                ) ,
                1 => array(
                    'foodNumber' => '1',
                    'portions' => '1',
                    'foodID' => '',
                ) ,
            ) ,
        ) ,
    ) ,
) ,
2 => array(
    0 => array(
        0 => array(
            'dietID' => '562',
            'blockNum' => '3',
        ) ,
        1 => array(
            0 => array(
                0 => array(
                    'mealTitle' => '',
                ) ,
                1 => array(
                    'foodNumber' => '1',
                    'portions' => '1',
                    'foodID' => '',
                ) ,
            ) ,
        ) ,
    ) ,
) ,
3 => array(
    0 => array(
        0 => array(
            'dietID' => '562',
            'blockNum' => '4',
        ) ,
        1 => array(
            0 => array(
                0 => array(
                    'mealTitle' => '',
                ) ,
                1 => array(
                    'foodNumber' => '1',
                    'portions' => '1',
                    'foodID' => '',
                ) ,
            ) ,
        ) ,
    ) ,
) ,
4 => array(
    0 => array(
        0 => array(
            'dietID' => '562',
            'blockNum' => '5',
        ) ,
        1 => array(
            0 => array(
                0 => array(
                    'mealTitle' => '',
                ) ,
                1 => array(
                    'foodNumber' => '1',
                    'portions' => '1',
                    'foodID' => '',
                ) ,
            ) ,
        ) ,
    ) ,
) ,
5 => array(
    0 => '',
) ,
6 => array(
    0 => '',
) ,
7 => array(
    0 => '',
) ,

);

/////编辑2

//////////////////解决方案//////////////////// < / p>

感谢@Brian的帮助,他让我意识到我的目标是错误的密钥,并且我试图创建一个不必要的额外阵列,我想出了解决这个问题的方法。

您可以在此处查看工作代码: http://codepad.org/6xgrpo46

希望这可以帮助将来的某个人! :)

1 个答案:

答案 0 :(得分:1)

当检查foodID是否为空时,您正在查看错误的值:

$get_food[$key][$key1][$key2][$key3]['foodID']

VS。

$get_food[$key][$key1][$key2][$key3][1]['foodID']

同样的问题似乎适用于您示例中的unset

foreach($get_food as $key => $meal){
    foreach($meal as $key1 => $mealpart){
        foreach($mealpart as $key2 => $food){
            foreach($food as $key3 => $fooditem){
                if ($key3 == 0)
                    continue;
                if (empty($get_food[$key][$key1][$key2][$key3][1]['foodID']))
                    unset($get_food[$key][$key1][$key2][$key3][1]);
            }
        }
    }
}