2016-05-17 142 views
0

我有以下表/ PHP數組;PHP將行數組轉換爲樹狀結構關聯數組

fileid | storage | parent | name | is_dir |  last_changed  | size | revision 
--------+---------+--------+------------+--------+----------------------------+-----------+---------- 
     1 |  1 |  | bunny.mov | f  | 2016-05-17 12:20:45.430934 | 514832018 |  103 
     2 |  1 |  | 10mb.bin | f  | 2016-05-17 12:24:11.291796 | 10000000 |  104 
     3 |  1 |  | 10mb.bin | f  | 2016-05-17 12:28:16.867 | 10000000 |  105 
     4 |  1 |  | bunny.mov | f  | 2016-05-17 12:34:42.191069 | 514832018 |  106 
     5 |  1 |  | miep2  | t  | 2016-05-17 12:38:09.286883 |  4096 |  107 
     6 |  1 |  5 | bunny.mov | f  | 2016-05-17 12:38:09.295631 | 514832018 |  107 
     7 |  1 |  | miep2  | t  | 2016-05-17 12:48:25.375968 |  4096 |  108 
     8 |  1 |  7 | bunany.mov | f  | 2016-05-17 12:48:25.384048 | 514832018 |  108 

我想在關聯數組中獲取這些數據,所以我可以構建一個樹狀結構。 (某些類型的文件瀏覽器,其中每個文件都有用戶可以選擇的修訂列表)

到目前爲止,我有以下代碼;

private function rebuildStructure($files, $parent) 
{ 
    $result = array(); 

    foreach ($files as $file){ 
     $entries = array(); 
     //get entries with $parent 
     foreach ($files as $entry_file){ 
      if ($entry_file->parent == $file->id){ 
       array_push($entries, $this->rebuildStructure($files, $entry_file->fileid)); 
      } 
     } 
     array_push($result, array(
      'name' => $file->name, 
      'entries' => $entries 
     )); 
    } 
    return $result; 
} 

但是,這並沒有工作(無限循環)。任何想法,我錯了? 我希望沿着一些方向行事;

array(
    array('name' => 'miep2', 
      'entries' => array(...and repeat...) 
    ) 
) 

有什麼建議嗎?謝謝!

+0

您在該方法的for循環中調用該方法,該方法確實以無限循環結束。另外,爲什麼如果你不打算使用''parent''參數給你的函數;) – Loek

回答

1

此代碼的意圖是以最簡單的方式實現解決方案,這是不高效的。

這個問題試圖找出陣列中每個元素的所有子元素。

private function rebuildStructure($files, $parent) 
{ 
    $result = array(); 

    foreach ($files as $file) { 
     // I'm searching for some childs, and I'm not one of them. 
     if (0 != $parent && $parent != $file->parent) continue; 

     // I'm a child and we are searching just for "parents" 
     if (!empty($file->parent) && 0 == $parent) continue; 

     $entries = array(); 

     // Next nesting level, search for children 
     array_push($entries, rebuildStructure($files, $file->fileid)); 

     array_push($result, array(
      'name' => $file->name, 
      'entries' => $entries 
     )); 
    } 

    return $result; 
} 
+0

這工作很好,謝謝@Miguel!您認爲什麼是更有效的方法? – Niels

+0

不需要在這個問題上使用遞歸,也不需要在數組上迭代很多次,只需要一次循環就可以更高效地完成樹。 –

+0

即使你不知道它有多深? – Niels

0

我想出了這個,它應該像你想要的那樣工作。

private function rebuildStructure($files) 
{ 
    $result = array(); 

    foreach ($files as $file) 
    { 
     $entries = array(); 

     foreach ($files as $entry_file) 
     { 
      if ($entry_file->parent === $file->id) 
      { 
       $nestedArray = array(); 
       array_push($nestedArray, $entry_file); 
       array_push($entries, $nestedArray); 
      } 
     } 
     array_push($result, array(
      'name' => $file->name, 
      'entries' => $entries 
     )); 
    } 
    return $result; 
} 

// Call this function to get the tree structure 
public function getRebuildedStructure($files) 
{ 
    return rebuildStructure($files); 
} 
+0

似乎原來的問題是使用遞歸,在這種情況下,兩級嵌套將不起作用。 –