Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to output Multidimensional Arrays PHP
    primarykey
    data
    text
    <p>I'm trying to create a multidimensional array which stores a website navigation. Currently my navigation consists of Main -> Level 1 -> Level 2. I am having issues accessing level 2 correctly. </p> <p>My Array example array is...</p> <pre><code> $pages = array ( array('page_name' =&gt; 'Home'), array('page_name' =&gt; 'About us', 'level_one' =&gt; array('first1','first2', 'level_two' =&gt; array('second'), 'first3')), array('page_name' =&gt; 'Gallery', 'level_one' =&gt; array('first1','first2','first3','first4')), array('page_name' =&gt; 'Contact us') ); </code></pre> <p>My code to retrieve the navigation so far is...</p> <p>$count = count($pages);</p> <pre><code>for ($x=0; $x&lt;$count; $x++) { # echo yes for active page if ($filename == $pages[$x][1]) { echo 'yes'; } # echo main navigation echo $pages[$x]['page_name']; # check if the item has a sub page $firstcount = count($pages[$x]['level_one']); # if the item has a sub page echo if ($firstcount &gt; 0) { for ($y=0; $y&lt;$firstcount; $y++) { echo "\r\n" . '.' . $pages[$x]['level_one'][$y]; # check if the page has a sub page $secondcount = count($pages[$x]['level_one']['level_two']); if ($secondcount &gt; 0) { for ($z=0; $z&lt;$secondcount; $z++) { if($pages[$x]['level_one']['level_two'][$z] != '') { echo "\r\n" . '..' . $pages[$x]['level_one']['level_two'][$z]; } }} } } echo "\r\n"; } </code></pre> <p>And the output I'm currently getting is...</p> <pre><code>Home About us .first1 ..second .first2 ..second .first3 ..second . ..second Gallery .first1 .first2 .first3 .first4 Contact us </code></pre> <p>I am trying to create a multi level navigation. My expected output would be...</p> <pre><code>&lt;!-- Expected output Home About us .first1 .first2 ..second .first3 Gallery .first1 .first2 .first3 .first4 Contact us --&gt; </code></pre>
    singulars
    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.
 

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