Skip to content

Instantly share code, notes, and snippets.

@md-riaz
Last active February 3, 2021 08:59
Show Gist options
  • Save md-riaz/3901ff7566de4f74779babb7b81e7eed to your computer and use it in GitHub Desktop.
Save md-riaz/3901ff7566de4f74779babb7b81e7eed to your computer and use it in GitHub Desktop.
<?php
$menu_array = [
[
'id' => 1,
'nav_name' => 'test/',
'parent_id' => 0
],
[
'id' => 2,
'nav_name' => 'test1/',
'parent_id' => 0
],
[
'id' => 3,
'nav_name' => 'test2/',
'parent_id' => 2
],
[
'id' => 4,
'nav_name' => 'test3/',
'parent_id' => 2
],
[
'id' => 5,
'nav_name' => 'test4/',
'parent_id' => 4
],
];
function buildMenuFromArray($menu_array, $parent_id = 0, $parentClass = "", $subClass = "")
{
$childItems = array();
foreach ($menu_array as $key => $item)
{
if ($item['parent_id'] == $parent_id)
{
$childItems[] = $item;
unset($menu_array[$key]);
}
}
if ($childItems)
{
echo "<ul class='{$parentClass}'>";
foreach ($childItems as $key => $item)
{
echo '<li>'.$item['nav_name'];
buildMenuFromArray($menu_array, $item['id'], $subClass, $subClass);
echo '</li>';
}
echo '</ul>';
} else {
echo "<ul class='{$subClass}'></ul>";
}
}
buildMenuFromArray($menu_array, 0, 'nav-table', 'sub');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment