How can one ensure proper indentation and hierarchy in dropdown menu options generated by PHP functions?
To ensure proper indentation and hierarchy in dropdown menu options generated by PHP functions, you can use recursive functions to handle nested menu items. By keeping track of the depth level of each menu item, you can add appropriate indentation or styling to visually represent the hierarchy.
function generateDropdownMenu($menuItems, $depth = 0) {
echo '<ul>';
foreach ($menuItems as $menuItem) {
echo '<li style="padding-left: ' . $depth * 10 . 'px;">' . $menuItem['label'] . '</li>';
if (isset($menuItem['children'])) {
generateDropdownMenu($menuItem['children'], $depth + 1);
}
}
echo '</ul>';
}
// Example usage
$menuItems = [
['label' => 'Home'],
['label' => 'About', 'children' => [
['label' => 'Team'],
['label' => 'History']
]],
['label' => 'Services']
];
generateDropdownMenu($menuItems);