从PHP平面数组建立了一棵树 [英] Build a tree from a flat array in PHP

查看:143
本文介绍了从PHP平面数组建立了一棵树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周互联网,还没有完全找到了我要找的。我有一个包含ID和PARENT_ID每个元素的平面数组。每个元素将仅具有一个父,但可以有多个孩子。如果PARENT_ID = 0,它被认为是根级别项目。我试图让我的平板阵列成树。我已经找到了其他样品也只是元素复制到父,但原仍然存在。

修改

起始阵列的每个元件是由一个单独的XML文件中读取。该文件本身将有0,作为PARENT_ID的值,如果它不具有父。键是实际的字符串。

我的混乱较早遗憾。希望这是更清楚:

/修改

我的出发数组:


排列

    [_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
            [儿童] =>阵列
                (
                    [_321_] =>阵列
                        (
                            [ID] => _321_
                            [PARENT_ID] => _320_
                            [儿童] =>阵列
                                (
                                    [_322_] =>阵列
                                        (
                                            [ID] => _322_
                                            [PARENT_ID] => _321_
                                        )
                                )
                        )
                    [_325_] =>阵列
                        (
                            [ID] => _325_
                            [PARENT_ID] => _320_
                        )
                )
    [_323_] =>阵列
        (
            [ID] => _323_
            [PARENT_ID] => 0
            [儿童] =>阵列
                (
                    [_324_] =>阵列
                        (
                            [ID] => _324_
                            [PARENT_ID] => _323_
                        )
                )
        )

任何帮助/指导大大AP preciated!

有些code我到目前为止有:

        功能buildTree(阵列&$元素,$ parentId的= 0){
        $分公司=阵列();        的foreach($元素作为$元素){
            如果($元素['PARENT_ID'] == $ parentId的){
                儿童$ = $这个 - > buildTree($元素,$元素['身份证']);
                如果($子女){
                    $元素['孩子'] = $儿童;
                }
                $分支[] = $元素;
            }
        }        返回$分公司;
    }


解决方案

您忘了未设置()在那里的兄弟。

 函数buildTree(阵列和放大器; $元素,$ parentId的= 0){
    $分公司=阵列();    的foreach($元素作为$元素){
        如果($元素['PARENT_ID'] == $ parentId的){
            $儿童= buildTree($元素,$元素['身份证']);
            如果($子女){
                $元素['孩子'] = $儿童;
            }
            $分支[$元素['身份证'] = $元素;
            未设置($元素[$元素['身份证']);
        }
    }
    返回$分公司;
}

I've looked around the internet and haven't quite found what I'm looking for. I have a flat array with each element containing an 'id' and a 'parent_id'. Each element will only have ONE parent, but may have multiple children. If the parent_id = 0, it is considered a root level item. I'm trying to get my flat array into a tree. The other samples I have found only only copy the element to the parent, but the original still exists.

EDIT

Each element of the starting array is read from a separate XML file. The file itself will have '0' as the value for parent_id if it doesn't have a parent. The keys are actually strings.

I'm sorry for the confusion earlier. Hopefully this is more clear:

/EDIT

My starting array:

Array
(
    [_319_] => Array
        (
            [id] => 0
            [parent_id] => 0
        )

    [_320_] => Array
        (
            [id] => _320_
            [parent_id] => 0
        )

    [_321_] => Array
        (
            [id] => _321_
            [parent_id] => _320_
        )

    [_322_] => Array
        (
            [id] => _322_
            [parent_id] => _321_
        )

    [_323_] => Array
        (
            [id] => _323_
            [parent_id] => 0
        )

    [_324_] => Array
        (
            [id] => _324_
            [parent_id] => _323_
        )

    [_325_] => Array
        (
            [id] => _325_
            [parent_id] => _320_
        )
)

The resulting array after the tree is made:

Array
(
    [_319_] => Array
        (
            [id] => _319_
            [parent_id] => 0
        )

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

Any help / guidance is greatly appreciated!

Some code I have so far:


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

        foreach ($elements as $element) {
            if ($element['parent_id'] == $parentId) {
                $children = $this->buildTree($elements, $element['id']);
                if ($children) {
                    $element['children'] = $children;
                }
                $branch[] = $element;
            }
        }

        return $branch;
    }

解决方案

You forgot the unset() in there bro.

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

这篇关于从PHP平面数组建立了一棵树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆