一尘不染

用PHP中的平面数组构建树

php

我环顾了互联网,但还没有完全找到想要的东西。我有一个平面数组,每个元素包含一个“ id”和一个“
parent_id”。每个元素只有一个父元素,但可能有多个子元素。如果parent_id =
0,则将其视为根级项目。我正在尝试将平面阵列变成一棵树。我发现的其他示例仅将元素复制到父元素,但原始元素仍然存在。

编辑

起始数组的每个元素都是从单独的XML文件中读取的。如果文件本身没有父文件,则其父文件ID的值将为‘0’。键实际上是字符串。

对不起,我很困惑。希望这更加清楚:

/编辑

我的起始数组:

数组
(
    [_319_] =>数组
        (
            [id] => 0
            [parent_id] => 0
        )

    [_320_] =>数组
        (
            [id] => _320_
            [parent_id] => 0
        )

    [_321_] =>数组
        (
            [id] => _321_
            [parent_id] => _320_
        )

    [_322_] =>数组
        (
            [id] => _322_
            [parent_id] => _321_
        )

    [_323_] =>数组
        (
            [id] => _323_
            [parent_id] => 0
        )

    [_324_] =>数组
        (
            [id] => _324_
            [parent_id] => _323_
        )

    [_325_] =>数组
        (
            [id] => _325_
            [parent_id] => _320_
        )
)

树之后的结果数组:

数组
(
    [_319_] =>数组
        (
            [id] => _319_
            [parent_id] => 0
        )

    [_320_] =>数组
        (
            [id] => _320_
            [parent_id] => 0
            [children] =>数组
                (
                    [_321_] =>数组
                        (
                            [id] => _321_
                            [parent_id] => _320_
                            [children] =>数组
                                (
                                    [_322_] =>数组
                                        (
                                            [id] => _322_
                                            [parent_id] => _321_
                                        )
                                )
                        )
                    [_325_] =>数组
                        (
                            [id] => _325_
                            [parent_id] => _320_
                        )
                )
    [_323_] =>数组
        (
            [id] => _323_
            [parent_id] => 0
            [children] =>数组
                (
                    [_324_] =>数组
                        (
                            [id] => _324_
                            [parent_id] => _323_
                        )
                )
        )

任何帮助/指导都将不胜感激!

到目前为止,我有一些代码:

        函数buildTree(array&$ elements,$ parentId = 0){
        $ branch = array();

        foreach(将$ elements作为$ element){
            如果($ element ['parent_id'] == $ parentId){
                $ children = $ this-> buildTree($ elements,$ element ['id']);
                如果($ children){
                    $ element ['children'] = $ children;
                }
                $ branch [] = $ element;
            }
        }

        返回$ branch;
    }

阅读 235

收藏
2020-05-26

共1个答案

一尘不染

你忘了unset()那里的兄弟。

function buildTree(array &$elements, $parentId = 0) {
    $branch = array();

    foreach ($elements as $element) {
        if ($element['parent_id'] == $parentId) {
            $children = buildTree($elements, $element['id']);
            if ($children) {
                $element['children'] = $children;
            }
            $branch[$element['id']] = $element;
            unset($elements[$element['id']]);
        }
    }
    return $branch;
}
2020-05-26