Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lcherone
Created December 24, 2017 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcherone/31e15bb2dde7b5f29cd0e0b881629a18 to your computer and use it in GitHub Desktop.
Save lcherone/31e15bb2dde7b5f29cd0e0b881629a18 to your computer and use it in GitHub Desktop.
nested recursive menu function

https://stackoverflow.com/questions/47957824/how-to-create-a-logic-repetition-for-branch-root-on-php/47958181

This is done by using a recursive function, and a nested array which you then traverse through. The following function could go n levels deep without issues.

<?php
function makeNav($item) {
    $ret = '<li><a href="'.$item['url'].'">'.$item['name'].'</a>'.PHP_EOL;

    if (isset($item['subPgs']) && is_array($item['subPgs']) && count($item['subPgs']) > 0) {
        $ret .= '<ul>'.PHP_EOL;
        foreach ($item['subPgs'] as $subPg) {
            $ret .= makeNav($subPg);
        }
        $ret .= '</ul>'.PHP_EOL;
    } else {
        $ret .= '</li>'.PHP_EOL;
    }

    if (isset($item['subPgs']) && is_array($item['subPgs']) && count($item['subPgs']) > 0) {
        $ret .= "</li>".PHP_EOL;
    }

    return $ret;
}

$navItems = array(
    /*array(
        'name' => 'Home',
        'url' => '#',
        'subPgs'=>array()
    ),*/
    array(
        'name' => 'Parent',
        'url' => '#',
        'subPgs' => array(
            array(
                'name' => 'Child',
                'url' => '#',
                'subPgs' => array(
                    array(
                        'name' => 'Grand Child',
                        'url' => '#'
                    ),
                    array(
                        'name' => 'Grand Child',
                        'url' => '#',
                        'subPgs' => array(
                            array(
                                'name' => 'Grand Child Child',
                                'url' => '#'
                            ),
                            array(
                                'name' => 'Grand Child Child',
                                'url' => '#'
                            )
                        )
                    )
                )
            ),
            array(
                'name' => 'Child',
                'url' => '#',
                'subPgs' => array(
                    array(
                        'name' => 'Grand Child',
                        'url' => '#',
                        'subPgs' => array(
                            array(
                                'name' => 'Grand Grand Child',
                                'url' => '#'
                            )
                        )
                    ),
                    array(
                        'name' => 'Grand Child',
                        'url' => '#'
                    )
                )
            )
        )
    )
);

$nav = '<ul>';
foreach ($navItems as $navItem) {
    $nav .= makeNav($navItem);
}
echo $nav.'</ul>';
?>

Outputs: (which matches OPs, except formatting)

<ul>
    <li>
        <a href="#">Parent</a>
        <ul>
            <li>
                <a href="#">Child</a>
                <ul>
                    <li>
                        <a href="#">Grand Child</a>
                    </li>
                    <li>
                        <a href="#">Grand Child</a>
                        <ul>
                            <li>
                                <a href="#">Grand Child Child</a>
                            </li>
                            <li>
                                <a href="#">Grand Child Child</a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li>
                <a href="#">Child</a>
                <ul>
                    <li>
                        <a href="#">Grand Child</a>
                        <ul>
                            <li>
                                <a href="#">Grand Grand Child</a>
                            </li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">Grand Child</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

See it online: https://3v4l.org/O4i2t

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment