首页 > 解决方案 > 如何使用 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;
}

哪个有效,但仅适用于顶级元素。我怎样才能进行递归并考虑多个级别的孩子?

标签: phparrays

解决方案


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当没有孩子时,键不存在。我通常希望它在那里并且值是一个空数组。


推荐阅读