EDIT: I figured it out nevermind
foreach (menu[$outer[$left]] as $item) {
echo '<dd><dd>'.$item.'<br>';
}
should be
foreach (menu[$outer][$left] as $item) {
echo '<dd><dd>'.$item.'<br>';
}
Hello this is pretty much my first time trying to use PHP with arrays so bear with me please....
I am having trouble looping through an array with nested arrays inside it.
Here's my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
//menu arrays
$menu = array(
'food'=>array(
'cheese'=>array(
'cheddar',
'mozerella'
),
'cracker'=>array(
'saltine'
),
'bread'=>array(
'white',
'wheat'
)
),
'drinks'=>array(
'coffee'=>array(
'decaf',
'caf'
),
'juice'=>array(
'apple',
'orange'
)
)
);
//loop through menu
foreach ($menu as $outer => $inner) {
echo $outer.'<br>';
foreach ($menu[$outer] as $left => $right) {
echo '<dd>'.$left.'<br>';
foreach (menu[$outer[$left]] as $item) {
echo '<dd><dd>'.$item.'<br>';
}
}
}
?>
I can loop through to see food, drinks, and cheese, cracker, etc., but I can't for the life of me get cheddar, mozerella, etc. to show up. I get this error:
Warning: Invalid argument supplied for foreach() for the third layer loop.
Am I forgetting something or is there just no way to loop through 3 nested arrays?