2016-05-17 139 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

似乎原来的问题是使用递归,在这种情况下,两级嵌套将不起作用。 –