0

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.

My Array example array is...

  $pages = array
  (
  array('page_name' => 'Home'),
  array('page_name' => 'About us', 'level_one' => array('first1','first2', 'level_two' => array('second'), 'first3')),
  array('page_name' => 'Gallery', 'level_one' => array('first1','first2','first3','first4')),
  array('page_name' => 'Contact us')
  );

My code to retrieve the navigation so far is...

$count = count($pages);

for ($x=0; $x<$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 > 0) { for ($y=0; $y<$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 > 0) { for ($z=0; $z<$secondcount; $z++) { 

            if($pages[$x]['level_one']['level_two'][$z] != '') { echo "\r\n" . '..' . $pages[$x]['level_one']['level_two'][$z]; }

        }}


    } }

    echo  "\r\n";


}

And the output I'm currently getting is...

Home
About us
.first1
..second
.first2
..second
.first3
..second
.
..second
Gallery
.first1
.first2
.first3
.first4
Contact us

I am trying to create a multi level navigation. My expected output would be...

<!-- Expected output

Home
About us
.first1
.first2
..second
.first3
Gallery
.first1
.first2
.first3
.first4
Contact us

-->
  • You are more likely to get helpful feedback if you explain what output you are expecting -- what's wrong with the output you posted? Also, you have lots of expressions like `[first]` and `[name]`. These are not valid, unless you have called `define()` to declare these as constants; you should use single or double quotes like `['first']` or `["first"]`. – elixenide Dec 29 '13 at 21:25
  • @EdCottrell I'm pretty sure the reason it's not working is because of what you just described, i.e. the keys not being enclosed in quotation marks. – Keshav Saharia Dec 29 '13 at 21:28
  • Thank you for the recommendations, I've updated the post so that it is more clear. – user3144659 Dec 29 '13 at 21:42
  • php will interpret undefined constants as strings, so [first] is equivalent to ['first'], unless first is defined as a constant somewhere else. php will note that it has done this for you in your error log though, so you'll get a lot of garbage notifications. – Noishe Dec 29 '13 at 22:05

3 Answers3

0

I think you should check out some php recursion examples:

function returnArrayAsString($ar, $level = 0){
  $str = '<ul class="level' . $level . '">';
  foreach($ar as $k => $v){
    if(is_array($v)){
      $str .= '<li>' . $k . returnArrayAsString($v, $level + 1) . '</li>';
    } else {
      $str .= '<li>' . $v . '</li>';
    }
  }
  $str .= '</ul>';
  return $str;
}

if you then have a simple array:

$ar = array(
   'Home',
   'About us' => array(
      'sub1a',
      'sub1b',
      'sub1c' => array(
         'sub2a',
         'sub2b'
      )
   )
);

Put array in function:

 echo returnArrayAsString($ar);

This would give you a nice ordered list no matter the depth of sub items in the array. As you can see, the function calls itself if it finds an array value. the $level variable can be used as a indicator, you could also use it for breadcrumbs ( About us > sub1c > sub2a).

Hope it helps....

verhie
  • 1,298
  • 1
  • 7
  • 7
0

The problem is in your loop code. Using foreach to make things more clear, results in the following, which outputs your result as you want it.

foreach($pages as $page)
{
    echo "{$page['page_name']}\n";
    foreach($page['level_one'] as $key => $level_one)
    {
        if($key === 'level_two')
        {
            foreach($level_one as $level_two)
            {
                echo "..$level_two\n";
            }
        }
        else
        {
            echo ".$level_one\n";
        }
    }
}
Noishe
  • 1,411
  • 11
  • 14
0

You can use a lot of functions/ways to do this: 1.By foreach(Reference)

2.use var_dump to view any arrayvar_dump()

But remember to use <pre></pre> for prettifying.

Community
  • 1
  • 1
Gun2sh
  • 870
  • 12
  • 22