通过父母和id的引用构建树

时间:2018-02-20 05:11:29

标签: php arrays recursion multidimensional-array reference

虽然我遇到了很多解决方案,但很明显它们都是基于递归的。如果有人可以解释并将此函数从递归转换为php引用,我将不胜感激。

// 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作为数组索引。这不是我想要的。例如这个

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),
)
如果有人可以查看它并通过使用引用而不是递归来帮助转换相同的函数,那么

会很感激。非常感谢!

1 个答案:

答案 0 :(得分:1)

可以在DbSimple中找到两遍实施。每个未知parent_id成为新的根,子元素将被放入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
                (
                )

        )
...