I've got this multidimensional array to display a menu that my website uses in some PHP code. I've been doing some experimenting and reading and I want to get this to work with a multidimensional array so that I can dynamically display the corresponding menu.
I have 3 different menus that can be displayed, "one", "two", and "three".
$pages = array(
'one' => array(
'about 1' => array('services 1', 'partners 1'),
'events 1',
'contact 1'
),
'two' => array(
'about 2' => array('services 2', 'partners 2'),
'events 2',
'contact 2'
),
'three' => array(
'about 3' => array('services 3', 'partners 3'),
'events 3',
'contact 3'
)
);
I have tried a few things so far, and I can't figure this out - I think my multidimensional array needs to be changed around?
Basically if $current_page == 'one' then I want to echo the corresponding array for 'one'.
It would have to be something like this:
<?php
echo '<ul class="menu">';
if ($current_page == 'one'){
foreach($pages['one'] as $page => $dropdown){
if (array has third dimension){ // pseudo code if pages have third dimension
echo '<li class="has-dropdown">' $page '</li>\n';
echo '<ul class="dropdown">';
foreach($pages['one'] as $page => $dropdown){
echo '<li>' . $dropdown . '</li>\n';
}
echo '</ul>';
} else { // no third dimension
foreach($pages['one'] as $page){
echo '<li>' $page '</li>\n';
}
}
}
}
elseif ($current_page == 'two'){
foreach($pages['two'] as $page){
if (array has third dimension){ // pseudo code if pages have third dimension
echo '<li class="has-dropdown">' $page '</li>\n';
echo '<ul class="dropdown">';
foreach($pages['one'] as $page => $dropdown){
echo '<li>' . $dropdown . '</li>\n';
}
echo '</ul>';
} else { // no third dimension
foreach($pages['one'] as $page){
echo '<li>' $page '</li>\n';
}
}
}
}
elseif ($current_page == 'three'){
foreach($pages['three'] as $page){
if (array has third dimension){ // pseudo code if pages have third dimension
echo '<li class="has-dropdown">' $page '</li>\n';
echo '<ul class="dropdown">';
foreach($pages['one'] as $page => $dropdown){
echo '<li>' . $dropdown . '</li>\n';
}
echo '</ul>';
} else { // no third dimension
foreach($pages['one'] as $page){
echo '<li>' $page '</li>\n';
}
}
}
}
echo '</ul>';
?>
The above doesn't work, it's just a rough picture of what I've been trying. The question is really: how do I use a foreach loop to display my menu? Any help is appreciated!