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

时间:2016-05-17 16:04:21

标签: php recursion parent

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

有什么建议吗?谢谢!

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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);
}