通过带有父母和ID的引用来构建树 [英] build tree by references with parents and id

查看:141
本文介绍了通过带有父母和ID的引用来构建树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我遇到了很多解决方案,但大多数情况下它们都是基于递归的.如果有人可以解释并将此功能从递归转换为php引用,我将不胜感激.

Although there are many solution i came across, mostely they are based on recursion. I would appreciate if someone can explain and convert this function from recursion to php references.

// build tree for Tree users
function buildTree(array $elements, $parentId = 0) {
    $branch = array();

    foreach ($elements as $element) {
        if ((string)$element['parent_id']  === (string)$parentId) {
            $children = buildTree($elements, $element['id']);
            if ($children) {
                $element['child'] = $children;
            }
            $branch[] = $element;
            // depthCommissionCalc($branch);  
        }
    }

    return $branch;
}

我确实尝试过自己,但是无法深入孩子的内心,因为我真的很了解php的引用概念.有许多通过php引用构建树的紧密解决方案,但是它们都保留ID或父ID作为数组索引.这不是我想要的.例如这个

i did tried myself but couldn't go into inner child depth because i am really knew to php references concept. there are many close solution to build tree by php references but all of them retain ID or parent Id as array index. which is not what i desire. for example this

function create($data){
    foreach($data as &$v){
        // Get childs
        if(isset($tree[$v['id']])) $v['child'] =& $tree[$v['id']];

        // push node into parent
        $tree[$v['parent_id']][$v['id']] =& $v;             

        // push child into node
        $tree[$v['id']]   =& $v['child'];
    }
    // return Tree
    return $tree[0];
}

,示例数组为

$test = array(
        array('id' => 1, 'name' => "a", 'parent_id' => 0),
        array('id' => 3, 'name' => "b", 'parent_id' => 1),
        array('id' => 2, 'name' => "c", 'parent_id' => 1),
        array('id' => 8, 'name' => "d", 'parent_id' => 0),
        array('id' => 4, 'name' => "e", 'parent_id' => 0),
        array('id' => 5, 'name' => "f", 'parent_id' => 0),
        array('id' => 6, 'name' => "i", 'parent_id' => 0),
        array('id' => 7, 'name' => "j", 'parent_id' => 0),
        array('id' => 11, 'name' => "k", 'parent_id' => 7),
        array('id' => 9, 'name' => "l", 'parent_id' => 0),
        array('id' => 10, 'name' => "m", 'parent_id' => 7),
        array('id' => 12, 'name' => "n", 'parent_id' => 7),
        array('id' => 13, 'name' => "o", 'parent_id' => 7),
        array('id' => 14, 'name' => "p", 'parent_id' => 10),
        array('id' => 15, 'name' => "q", 'parent_id' => 10),
        array('id' => 16, 'name' => "r", 'parent_id' => 15),
        array('id' => 17, 'name' => "s", 'parent_id' => 16),
        array('id' => 18, 'name' => "t", 'parent_id' => 17),
)

如果有人可以研究它,并通过使用引用(而不是递归)来帮助转换相同的函数,也将感到感谢.非常感谢!

would appreciate if someone can look into it and help converting the same function by using references instead of recursion and explain as well. Many Thanks!

推荐答案

可以在

Two-pass implementation can be found in DbSimple. Every unknown parent_id become new root, child elements will be placed into childNodes:

$test = array(
        array('id' => 1, 'name' => "a", 'parent_id' => 0),
        array('id' => 3, 'name' => "b", 'parent_id' => 1),
        array('id' => 2, 'name' => "c", 'parent_id' => 1),
        array('id' => 8, 'name' => "d", 'parent_id' => 0),
        array('id' => 4, 'name' => "e", 'parent_id' => 0),
        array('id' => 5, 'name' => "f", 'parent_id' => 0),
        array('id' => 6, 'name' => "i", 'parent_id' => 0),
        array('id' => 7, 'name' => "j", 'parent_id' => 0),
        array('id' => 11, 'name' => "k", 'parent_id' => 7),
        array('id' => 9, 'name' => "l", 'parent_id' => 0),
        array('id' => 10, 'name' => "m", 'parent_id' => 7),
        array('id' => 12, 'name' => "n", 'parent_id' => 7),
        array('id' => 13, 'name' => "o", 'parent_id' => 7),
        array('id' => 14, 'name' => "p", 'parent_id' => 10),
        array('id' => 15, 'name' => "q", 'parent_id' => 10),
        array('id' => 16, 'name' => "r", 'parent_id' => 15),
        array('id' => 17, 'name' => "s", 'parent_id' => 16),
        array('id' => 18, 'name' => "t", 'parent_id' => 17),
);


$forest = _transformResultToForest($test, 'id', 'parent_id');
print_r($forest);

输出为:

Array
(
    [1] => Array
        (
            [name] => a
            [childNodes] => Array
                (
                    [3] => Array
                        (
                            [name] => b
                            [childNodes] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [name] => c
                            [childNodes] => Array
                                (
                                )

                        )

                )

        )

    [8] => Array
        (
            [name] => d
            [childNodes] => Array
                (
                )

        )
...

这篇关于通过带有父母和ID的引用来构建树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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