0

I want to turn the following array into a nested list in html.

$aMenu = array(
array("name" => "Page 1", 
      "url" => "http://www.microsoft.se", 
      "subpages" => array(
        array("name" => "Subpage 1.1", "url" => "http://www.reddit.com"),
        array("name" => "Subpage 1.2", "url" => "http://www.google.se"),          
        array("name" => "Subpage 1.3", "url" => "http://www.cnn.com"),
    )               
),          
array("name" => "Page 2", "url" => "http://www.facebook.com"),
array("name" => "Page 3",
      "url" => "http://www.bbc.co.uk",
      "subpages" => array(
          array("name" => "Subpage 3.1", "url" => "http://www.jamesedition.com"),
          array("name" => "Subpage 3.2", "url" => "http://www.huffpost.com"),
          array("name" => "Subpage 3.3", 
                "url" => "http://www.lagunitas.com",
                "subpages" => array(
                    array("name" => "Subpage 3.3.1", "url" => "http://www.burton.com"),
                    array("name" => "Subpage 3.3.2", "url" => "http://www.hm.com"),
                    array("name" => "Subpage 3.3.3", "url" => "http://www.apple.com"),
                ),
          ),
      ),                                
),
array("name" => "Page 4", "url" => "http://www.instagram.com"),
array("name" => "Page 5", "url" => "http://www.flickr.com"),
);

Desired output:

  • Page 1
    • Subpage 1.1
    • Subpage 1.2
    • Subpage 1.3
  • Page 2
  • Page 3
    • Subpage 3.1
    • Subpage 3.2
    • Subpage 3.3
      • Subpage 3.3.1
      • Subpage 3.3.2
      • Subpage 3.3.3
  • Page 4
  • Page 5

I know how to do this manually and I know how to loop through single levels of the array but I cannot figure out how to loop through it all and output the results into nested lists. I have seen separate solutions to these problems but I can't seem to make them work together. Any help would be appreciated.

2 Answers 2

1

PHP speudo-code:

function list2html($list)
{
  $html = '';
  foreach ($list as $item)
  {
    $html .= '<li>' . $item['name'];
    if ( !empty($item['subpages']) ) // recurse here
      $html .= list2html($item['subpages']);
    $html .= '</li>';
  }
   return '<ul>' . $html . '</ul>';
}

Use like this:

$html_output = list2html($aMenu);

Note urls are missing you will have to modify the function to add them

Note2 Recursion is not needed, one can iterate (or simulate recursion with iteration) and it will be faster but more complex code

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

2 Comments

Thanks Nikos, this seems to work but it outputs the lists separately instead of as nested. I will play around with it and see if I can get it to output a nested list. Thanks again!
@BrianRanney, the html list is nested (because each subpages are rendered inside the current li element), i would suggest to change your css
0

You have to make recursive function, which will go trough the one level of array and call it self for every array found as the parameter.

3 Comments

Thanks for the quick reply! I am still a little new with PHP can you explain what you mean in a little more depth?
You have to make a function that will have array as a parameter. Function should iterate trough that array (i.e. with foreach command) and check for every element found. If element is simple (not an array) function should print out it's html. But if it's array function should call it self and pass that array as a parameter. At beginning you should call the function with initial array.
Ok, that sounds logical. I haven't had a chance to try it out yet but I will see what I can come up with. Thanks!

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.