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! :)
var_export). We can't test our answers with vardumps!var_exportnow.