2012-08-03 113 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