2012-02-15 102 views
2

我有一個類別數組,其中id是類別的id,parent表示該類別的父級id(id 0表示最上面的父節點),value是陣列。路徑最初設置爲類別的ID。該陣列如下:沒有分配給變量的值

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [parent] => 0 
      [value] => Corporate Files 
      [path] => 1 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [parent] => 0 
      [value] => Products Files 
      [path] => 2 
     ) 

    [2] => Array 
     (
      [id] => 3 
      [parent] => 1 
      [value] => Communications Materials 
      [path] => 3 
     ) 

    [3] => Array 
     (
      [id] => 4 
      [parent] => 1 
      [value] => Group Technical 
      [path] => 4 
     ) 

    [4] => Array 
     (
      [id] => 5 
      [parent] => 1 
      [value] => New Projects 
      [path] => 5 
     ) 

    [5] => Array 
     (
      [id] => 6 
      [parent] => 2 
      [value] => Product Range 
      [path] => 6 
     ) 

    [6] => Array 
     (
      [id] => 7 
      [parent] => 2 
      [value] => WL4 
      [path] => 7 
     ) 
); 

我想生成數組中的類別路徑。所以輸出應該是

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [parent] => 0 
      [value] => Corporate Files 
      [path] => 1 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [parent] => 0 
      [value] => Products Files 
      [path] => 2 
     ) 

    [2] => Array 
     (
      [id] => 3 
      [parent] => 1 
      [value] => Communications Materials 
      [path] => 1,3 
     ) 

    [3] => Array 
     (
      [id] => 4 
      [parent] => 1 
      [value] => Group Technical 
      [path] => 1,4 
     ) 

    [4] => Array 
     (
      [id] => 5 
      [parent] => 1 
      [value] => New Projects 
      [path] => 1,5 
     ) 

    [5] => Array 
     (
      [id] => 6 
      [parent] => 2 
      [value] => Product Range 
      [path] => 2,6 
     ) 

    [6] => Array 
     (
      [id] => 7 
      [parent] => 2 
      [value] => WL4 
      [path] => 2,7 
     ) 
); 

我寫了下面的函數。

function findparent($id,$path){ 
     global $categories; 
     global $catcnt; 

     if($id==0){ 
      echo $path."<br />"; //this outputs path currently 
      return $path; 
     } 
     for($i=0;$i<$catcnt;$i++){ 

      if($id==$categories[$i]['id']){ 
       $path=$id.",".$path; 
       findparent($categories[$i]['parent'],$path); 
      } 
     } 
    } 

for($i=0;$i<count($categories);$i++){ 
      $categories[$i]['path']=(string)findparent($categories[$i]['parent'],$categories[$i]['id']); //this doesnt assign it currectly 

    } 

,輸出是:

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [parent] => 0 
      [value] => Corporate Files 
      [path] => 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [parent] => 0 
      [value] => Products Files 
      [path] => 
     ) 

    [2] => Array 
     (
      [id] => 3 
      [parent] => 1 
      [value] => Communications Materials 
      [path] => 
     ) 

    [3] => Array 
     (
      [id] => 4 
      [parent] => 1 
      [value] => Group Technical 
      [path] => 
     ) 

    [4] => Array 
     (
      [id] => 5 
      [parent] => 1 
      [value] => New Projects 
      [path] => 
     ) 

    [5] => Array 
     (
      [id] => 6 
      [parent] => 2 
      [value] => Product Range 
      [path] => 
     ) 

    [6] => Array 
     (
      [id] => 7 
      [parent] => 2 
      [value] => WL4 
      [path] => 
     ) 
); 

我要去哪裏錯了?

+0

我想你會在'findparent'函數內出錯。另請參見我對[根據父ID值將數組從一個數據轉換爲多維]的回答(http://stackoverflow.com/a/7768445/367456),它類似並顯示/說明如何收集數據。 – hakre 2012-02-15 15:10:27

回答

2

findparent僅當id爲零時才返回。

在遞歸調用findparent之前,您需要第二個return語句。

+0

謝謝tonne :) – 2012-02-15 15:21:15

1

當你不需要處理多層次,它自己的功能是一個有點開銷,你可以在陣列本身上直接運行一個簡單的foreach

foreach ($array as &$node) 
{ 
     if ($node['parent']) 
     { 
       $node['path'] = $node['parent'] . ',' . $node['path']; 
     } 
} 
unset($node); 

然而,你可以把它放到它自己的函數中,但是就我所見,你不需要任何全局變量。

你在這裏看到的是簡單的字符串連接,這使得你寫的數組就像你想要的那樣。我的第一條評論意味着更深入地管理這個結構,而不是一個深度。 Demo