Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think you should get into objects. Plus I think it would just be extra work to generate objects and etc. In my opinion you should loop through the array and generate a multidimensional array that represents the navigational hierarchy and then loop the generated array recursively to generate your HTML. I've done a sample code for you, it works the way you want it to but you probably want to make some changes.</p> <p><strong>functions</strong></p> <pre><code>// Generate your multidimensional array from the linear array function GenerateNavArray($arr, $parent = 0) { $pages = Array(); foreach($arr as $page) { if($page['parent'] == $parent) { $page['sub'] = isset($page['sub']) ? $page['sub'] : GenerateNavArray($arr, $page['id']); $pages[] = $page; } } return $pages; } // loop the multidimensional array recursively to generate the HTML function GenerateNavHTML($nav) { $html = ''; foreach($nav as $page) { $html .= '&lt;ul&gt;&lt;li&gt;'; $html .= '&lt;a href="' . $page['link'] . '"&gt;' . $page['name'] . '&lt;/a&gt;'; $html .= GenerateNavHTML($page['sub']); $html .= '&lt;/li&gt;&lt;/ul&gt;'; } return $html; } </code></pre> <p>** sample usage **</p> <pre><code>$nav = Array ( Array ( 'id' =&gt; 27, 'name' =&gt; 'home', 'link' =&gt; 'home.html', 'parent' =&gt; 0 ), Array ( 'id' =&gt; 30, 'name' =&gt; 'about', 'link' =&gt; 'about.html', 'parent' =&gt; 27 ) ); $navarray = GenerateNavArray($nav); echo GenerateNavHTML($navarray); </code></pre> <p>You can probably do both things in one step but I think it's neater to generate the multidimensional array first. Goodluck!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload