PHP 将行数组转换为树结构关联数组

PHP convert array of rows to tree-structured associative array

我有以下 table/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... )
    )
)

有什么建议吗?谢谢!

我想到了这个,它应该可以像您希望的那样工作。

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);
}

这段代码的目的是用最简单的方式实现解决方案,这样效率不高。

这个方法试图找到数组中每个元素的所有子元素。

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;
}