2013-08-01 62 views
1

我需要遞歸下拉菜單一點幫助。遞歸菜單,寫下所有父母和孩子

這裏是我的數組:

Array 
(
    [0] => Array 
     (
      [cat_id] => 4 
      [cat_name] => Plátna 
      [cat_nodia] => Platna 
      [cat_child] => 0 
      [cat_desc] => 
      [cat_main] => 1 
      [cat_order] => 4 
      [parent] => 
      [sub] => Array 
       (
        [0] => Array 
         (
          [cat_id] => 16 
          [cat_name] => Ľanové 
          [cat_nodia] => Lanove 
          [cat_child] => 4 
          [cat_desc] => 
          [cat_main] => 0 
          [cat_order] => 16 
          [parent] => Ľanové 
          [sub] => 
         ) 

        [1] => Array 
         (
          [cat_id] => 17 
          [cat_name] => Bavlnené 
          [cat_nodia] => Bavlnene 
          [cat_child] => 4 
          [cat_desc] => 
          [cat_main] => 0 
          [cat_order] => 17 
          [parent] => Bavlnené 
          [sub] => Array 
           (
            [0] => Array 
             (
              [cat_id] => 21 
              [cat_name] => 10x10 
              [cat_nodia] => 10x10 
              [cat_child] => 17 
              [cat_desc] => 
              [cat_main] => 0 
              [cat_order] => 21 
              [parent] => 10x10 
              [sub] => 
             ) 

            [1] => Array 
             (
              [cat_id] => 22 
              [cat_name] => 20x25 
              [cat_nodia] => 20x25 
              [cat_child] => 17 
              [cat_desc] => 
              [cat_main] => 0 
              [cat_order] => 22 
              [parent] => 20x25 
              [sub] => 
             ) 

           ) 

         ) 

       ) 

     ) 

    [1] => Array 
     (
      [cat_id] => 5 
      [cat_name] => Uhlík 
      [cat_nodia] => Uhlik 
      [cat_child] => 0 
      [cat_desc] => 
      [cat_main] => 0 
      [cat_order] => 5 
      [parent] => 
      [sub] => 
     ) 
) 

而且我想使下拉菜單這應該是這樣的:

Platna 
Platna - Lanove 
Platna - Bavlnene 
Platna - Bavlnene - 10x10 
Platna - Bavlnene - 20x25 
Uhlik 

這是我到目前爲止已經試過:

function show_menu($array) 
    { 
    // print_r($array); 
     foreach($array as $val) 
     { 
      if (is_array($val['sub'])) 
      { 
       echo $val['cat_name']; 
       $this->show_menu($val['sub']); 
      } else 
      { 
       echo $val['cat_name']; 
      } 
      echo "<br>"; 
     } 
    } 

這給了我這樣的smh:

Plátna Ľanové 
Bavlnené 10x10 
20x25 

感謝所有幫助

+0

我認爲這會幫助你。 http://wizardinternetsolutions.com/articles/web-programming/single-query-dynamic-multi-level-menu – Moylin

+0

我期望的輸出是什麼? – Baba

+0

我在代碼之間提到它 – DeiForm

回答

1

試試這個

function show_menu($array, $name='') 
    { 
    // print_r($array); 
     foreach($array as $val) 
     { 
      echo $name . $val['cat_name'] . "<br>"; 
      if (is_array($val['sub'])) 
      { 
       $this->show_menu($val['sub'], $name . $val['cat_name'] . ' - '); 
      } 
     } 
    } 
+0

工作,但你首先回聲缺少'''。:)謝謝 – DeiForm