提问者:小点点

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


我知道这对其他问题来说可能是重复的,但我一直在测试其他问题的解决方案,我无法让它们在我的特定情况下发挥作用。

我有一个复杂的多维数组,叫做$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\u导出

$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

//////////////////解决方案////////////////////

感谢@Brian的帮助,他让我意识到我瞄准了错误的键,我试图创建一个不必要的额外数组,我想出了这个问题的解决方案。

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

希望这对未来的人有帮助!:)


共1个答案

匿名用户

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

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