dynamically create PHP navigation menu from multid

2019-08-16 06:47发布

I have seen so many examples but wasn't able to find an exact answer. Most of the question answered either have errors of are not answered properly. If anyone knows of a finished solution similar to my question, please point me in that direction!

   $menu = Array
(
    [dashboard] => Array
        (
            [main] => /phone/summary
            [main_selected] => 1
            [child_selected] => 
            [children] => Array
                (
                    [phones] => /phones
                    [tablets] => /tablets
                )

        )

    [mobile templates] => Array
        (
            [main] => /phone/templates
            [main_selected] => 
        )

    [phone size] => Array
        (
            [main] => link_3
            [main_selected] => 
            [child_selected] => 
            [children] => Array
                (
                    [iphone] => /phone/iphone_size
                    [samsung] => /phone/samsung_size
                    [android] => /phone/android_size
                    [blackberry] => /phone/blackberry_size
                    [google_pixels] => /phone/google_phone_size
                )

        )

    [phone chart] => Array
        (
            [main] => /phone/charts/
            [main_selected] => 
        )


)

And here is what I would like to turn it into:

    <ul>
    <li><a href="">Dashboard<a/>
        <ul>
            <li><a href="/phones">Iphone</a></li>
            <li><a href="/tablets">Tablets</a></li>
        </ul>
    </li>
    <li><a href="/phone/templates">mobile templates<a/>
    </li>
    <li><a href="link_3">phone size<a/>
        <ul>
            <li><a href="/phone/iphone_size">Iphone</a></li>
            <li><a href="/phone/samsung_size">Samsung</a></li>
            <li><a href="/phone/android_size">Android</a></li>
            <li><a href="/phone/blackberry_size">Blackberry</a></li>
            <li><a href="/phone/google_phone_size">Google_pixels</a></li>
        </ul>
    </li>
    <li><a href="/phone/charts/">phone chart<a/></li>
</ul>

Hope someone understands what is displayed. This is what I have tried. Sure it's not very great.

public function sideMenu($actions, $level = 0)
    {
        $xhtml = '';
        $main_menu = null;
        $ret = "";
        $test = "";

        foreach ($actions as $key => $value) 
        {    
            if(isset($value['children']))
            {
                $main_menu .= "<li>".$key."<li>";
                //print_r($main_menu);

                if (is_array($value["children"])) 
                {
                    $ret .= "<ul>";
                    $test = $value["children"];
                    foreach ($test as $k1 => $v1) 
                    {

                        $ret .= "<li> ".$k1." </li>";


                    }
                    $ret .= "</ul>";
                    print_r($ret);
                }


            }
            else
            {
                $main_menu .= "<li>".$key."</li>";
            }   

        }


        return $xhtml;
    }

1条回答
可以哭但决不认输i
2楼-- · 2019-08-16 06:49
public function sideMenu($actions){
    $main_menu = "<ul>";
    foreach ($actions as $key => $value) 
    {    
        $main_menu .= "<li><a href='".$value['main']."'>".$key."</a>";
        if(isset($value['children']) && is_array($value['children'])){
            $main_menu .= "<ul>";
            foreach($value['children'] as $k => $v){
                $main_menu .= "<li><a href='".$v."'>".$k."</a></li>";
            }
            $main_menu .= "</ul>";
        }
        $main_menu .= "</li>";
    }

    $main_menu .= "</ul>";
    return $main_menu;
}

I have updated your sideMenu function and you can call it by echo $obj->sideMenu($menu); Where $obj is your class object where this function is defined.

Live Demo

查看更多
登录 后发表回答