0

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;
    }
5
  • Share what you have tried till yet? Commented May 15, 2017 at 10:16
  • It will need a lot of codes to parse your array and generate the HTML. You can try writing the codes. Commented May 15, 2017 at 10:17
  • this is a lot of work kind of. You need to try something, then we can help Commented May 15, 2017 at 10:17
  • Don't actually know where to start as am new to PHP Commented May 15, 2017 at 10:40
  • You could start with a valid Array. Then you are creating $main_menu but don't seem to display it. Commented May 15, 2017 at 10:43

1 Answer 1

0
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

Sign up to request clarification or add additional context in comments.

5 Comments

Just thinking. Is there a different way to building up the code to bring the same result.
It is understandable customized and optimized solution for a two-level menu of your array. If you have the same structure for parent and child menu item we can call the same function recursively for n level.
Would you just add an extra foreach, if you are dealing with a three-level menu
If array structure remains as same as parent array, we do not need extra foreach. If array structure is as same as like current array, we must add extra foreach.
Because, Current array contains 'Menu Value'=>array( 'main' => URL); and childern contains 'Menu Value'=>URL;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.