0

I know this might seem like a duplicate for other questions out there, but I've been testing the solutions for the other questions and I can't get them to work in my specific situation.

I have a "complex" multidimiensional array called $get_food that looks like this:

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

I need to iterate thru it and remove the sub arrays that have an empty [foodID].

e.g. Remove the whole sub-array with the key [5] because it has an empty [foodID] item and it's useless that way:

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

And once those sub arrays are deleted, I need to reconstruct the main array so it looks the same than it did originally (with the same levels), but without the unwanted sub-arrays.

I managed to do this on a much simpler multidimensional array, but not this time around. I can't get it to look the same at the end.

This is the messy code post-frustration that I have right now that just outputs nonsense:

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;

So, any help is very much appreciated :)

//////// EDIT

The main array is put together from 8 arrays. Here's the var_export for them together as one:

$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 => '',
) ,

);

///// EDIT 2

////////////////// SOLUTION ////////////////////

Thanks to @Brian 's help, who made me realize I was targeting the wrong keys and that I was trying to create an additional array that was not necessary I came up with the solution to this issue.

You can see the working code here: http://codepad.org/6xgrpo46

Hope this helps someone in the future! :)

2
  • 1
    It would be more helpful to post your array in a testable form (var_export). We can't test our answers with vardumps! Commented Jun 10, 2014 at 23:34
  • @thg435 Thank you for noting that! I've added the var_export now. Commented Jun 10, 2014 at 23:40

1 Answer 1

1

When checking if the foodID is empty or not, you are looking at the wrong value:

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

vs.

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

And the same problem seems to be for the unset in your example.

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]);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Oh, yeah! It was targeting the wrong key. That unsets the empty element. But how do I output the updated array?
I'm not so sure what you mean here. $get_food was mutated by running through the array using foreach and using unset on one of its members. Just access it with $get_food. Is there something I'm missing?
I edited the question with my results. I'm pretty sure it's my mistake, but I can't figure out what is it exactly :(
It was working on the exact array that you gave (from var_export), here: codepad.org/NTyfm5vB -- can you double check that the layout of $get_food matches?
The output still has sub-arrays with empty [foodID] elements.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.