如何用PHP提取多维数组树的一部分

亚零

我有一个巨大的动态生成的树。该树是根据每个元素的“parent_id”属性从平面数组生成的。

例如,最终结果如下所示:

Array
(
    [0] => Array
        (
            [id] => 70
            [name] => Top Corp
            [parent_id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 43
                            [name] => Department
                            [parent_id] => 70
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 45
                                            [name] => Building
                                            [parent_id] => 43
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 75
                                                            [name] => Office
                                                            [parent_id] => 45
                                                        )

                                                )

                                        )

如何只提取数组树的一部分?我应该查看哪些函数或方法?

例如,我怎么说另一个子级别(可能深 20-30 级)现在是顶部。

例如, 的伪函数sliceTree(45)应该产生以下结果,也就是从id 45

[0] => Array
    (
        [id] => 45
        [name] => Building
        [parent_id] => 43
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 75
                        [name] => Office
                        [parent_id] => 45
                    )

            )

    )

没有办法知道树可以走多深,因此解决方案需要递归。

我尝试循环数组,寻找起始 id,但我不确定在找到点后如何继续执行。

我提出的解决方案如下

function sliceTree($tree, $id){
    $ret = [];
    foreach ($tree as $out) {
        // if the top level matches
        if($out["id"] == $id){
            array_push($ret, $out);
        }
        else {
            if(isset($out["children"])){
                foreach ($out["children"] as $c) {
                    if($c["id"] == $id){
                        array_push($ret, $c);
                    }
                   // probably needs to call itself here
                }
            }
        }
    }
    return $ret;
}

哪个有效,但仅适用于顶级元素。如何进行递归并考虑多个级别的子级?

KIKO软件

sliceTree()函数基本上查找某个id并返回它。像这样的东西:

function sliceTree($tree, $branchId)
{
    // check all branches
    foreach ($tree as $branch) {
        // have we found the correct branch?
        if ($branch['id'] == $branchId) return $branch;
        // check the children
        if (isset($branch['children'])) {
            $slice = sliceTree($branch['children'], $branchId);
            if (isset($slice)) return $slice;
        } 
    }
    // nothing was found
    return null;
}

如您所见,此例程是递归的。代码未经测试。

我很抱歉混合的比喻:树枝和孩子,但你开始了它。

这个函数比我希望的要复杂一些,因为在你的例子中,children当没有孩子时键不存在。我通常希望它在那里并且值是一个空数组。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章