2012-08-03 79 views
1

我已經在這兩天了,現在我完全卡住了。谷歌,因此無法幫我,我忽視的東西....列出來自多維陣列的孩子父母

這就是我想要

Home 
Typography 
404 Page not found 
The company 
The company/Team 
The company/Team/Rick 
The company/Team/Rick/Opleiding 
The company/Team//Rob 
The company/Office 
The company/Office/Contact 
The company/Office/Route 

這是我的數組

Array 
(
    [0] => Array 
     (
      [id] => 55 
      [parent_id] => 0 
      [title] => Home 
      [sub] => Array 
       (
       ) 

     ) 

    [1] => Array 
     (
      [id] => 27 
      [parent_id] => 0 
      [title] => Typography 
      [sub] => Array 
       (
       ) 

     ) 

    [2] => Array 
     (
      [id] => 56 
      [parent_id] => 0 
      [title] => 404 Page not found 
      [sub] => Array 
       (
       ) 

     ) 

    [3] => Array 
     (
      [id] => 68 
      [parent_id] => 0 
      [title] => The company 
      [sub] => Array 
       (
        [0] => Array 
         (
          [id] => 73 
          [parent_id] => 68 
          [title] => Team 
          [sub] => Array 
           (
            [0] => Array 
             (
              [id] => 74 
              [parent_id] => 73 
              [title] => Rick 
              [sub] => Array 
               (
                [0] => Array 
                 (
                  [id] => 79 
                  [parent_id] => 74 
                  [title] => Opleiding 
                  [sub] => Array 
                   (
                   ) 

                 ) 

               ) 

             ) 

            [1] => Array 
             (
              [id] => 75 
              [parent_id] => 73 
              [title] => Rob 
              [sub] => Array 
               (
               ) 

             ) 

           ) 

         ) 

        [1] => Array 
         (
          [id] => 76 
          [parent_id] => 68 
          [title] => Office 
          [sub] => Array 
           (
            [0] => Array 
             (
              [id] => 78 
              [parent_id] => 76 
              [title] => Contact 
              [sub] => Array 
               (
               ) 

             ) 

            [1] => Array 
             (
              [id] => 77 
              [parent_id] => 76 
              [title] => Route 
              [sub] => Array 
               (
               ) 

             ) 

           ) 

         ) 

       ) 

     ) 

) 

這是我至今

public function BuildTitle($array, &$parents = null) 
    { 
     $html = ''; 
     foreach($array as $page) 
     { 
      if($page['parent_id'] != 0) 
      { 
       $html .= $parents.'/'.$page['title'].'<br/>'; 
       $parents = $parents.'/'.$page['title']; 
      } 
      else 
      { 
       $html .= $page['title'].'<br/>'; 
       $parents = $page['title']; 
      } 

      $html .= $this->BuildTitle($page['sub'], $parents); 
     } 
     return $html; 
    } 

並返回

Home 
Typography 
404 Page not found 
The company 
The company/Team 
The company/Team/Rick 
The company/Team/Rick/Opleiding 
The company/Team/Rick/Opleiding/Rob 
The company/Team/Rick/Opleiding/Rob/Office 
The company/Team/Rick/Opleiding/Rob/Office/Contact 
The company/Team/Rick/Opleiding/Rob/Office/Contact/Route 

父母/子女關係是無限的。我做錯了什麼/失蹤?提前致謝!

回答

0

您正在修改每次迭代中相同的$parents值。所以,只要父母不是0,它就會將當前孩子追加到任何路徑。

想象一下,比如你有以下情形:

foo 
    bar 
    baz 

當處理foo,它會開始$parents = 'foo'。 它然後在每個孩子的推移,更新$parents

  • bar使得$parents = $parents . '/' . $page['title'] = 'foo' . '/' . 'bar' = 'foo/bar'
  • baz使得$parents = $parents . '/' . $page['title'] = 'foo/bar' . '/' . 'bar' = 'foo/bar/baz'

你想是什麼:

public function BuildTitle($array, $parents = null) 
    { 
     $html = ''; 
     foreach($array as $page) 
     { 
      if($page['parent_id'] != 0) 
      { 
       $html .= $parents.'/'.$page['title'].'<br/>'; 
       $new_parents = $parents.'/'.$page['title']; 
      } 
      else 
      { 
       $html .= $page['title'].'<br/>'; 
       $new_parents = $page['title']; 
      } 

      $html .= $this->BuildTitle($page['sub'], $new_parents); 
     } 
     return $html; 
    } 
+0

你先生是真棒!我只是試圖改變$的父母變種,但從來沒有想過傳遞一個新的變種。謝謝! – 2012-08-03 12:17:40