递归并通过引用传递

sev*_*ers 7 php recursion pass-by-reference

我有一个以下结构类别的树:

[6] => Array
    (
        [id] => 6
        [name] => computers
        [productCount] => 0
        [children] => Array
            (
                [91] => Array
                    (
                        [id] => 91
                        [name] => notebook
                        [productCount] => 5
                        [children] => Array
                            (
                            )
                    )

                [86] => Array
                    (
                        [id] => 86
                        [name] => desktop
                        [productCount] => 0
                        [children] => Array
                            (
                            )
                    )
            )
    )
Run Code Online (Sandbox Code Playgroud)

除了子类别,每个类别可能包含产品(如文件夹可能包含子文件夹和文件).

我正在尝试编写一个递归函数,我希望将此数组作为参考,并使用[productCount] = 0和包含此类空节点的所有父类别剥离两个叶类别.换句话说,在处理之后,我想只有那些在任何子级上持有产品的类别.

我已经写了一些代码,现在调试它并且它不会剥离空节点.可能是我没有正确使用引用.如果可能的话,请帮我修理一下.

    function pruneTree( & $node) {
    if ( ! $node['children'] && ! $node['productCount']) {
        unset($node);
    }
    if ( ! empty($node['children'])) {
        foreach ($node['children'] as $key => $child) {
            pruneTree($node['children'][$key]);
        }
    }
    return;
}
Run Code Online (Sandbox Code Playgroud)

Gum*_*mbo 5

unset 仅删除引用但不删除引用的变量:

如果PASSED BY REFERENCE的变量unset()位于函数内部,则仅销毁局部变量.调用环境中的变量将保留与之前unset()调用的值相同的值.

所以你需要传递父数组和密钥来删除该变量:

function pruneTree(&$parent, $key) {
    $node = &$parent[$key];
    if (!$node['children'] && !$node['productCount']) {
        unset($parent[$key]);
    }
    if (!empty($node['children'])) {
        foreach ($node['children'] as $key => &$child) {
            pruneTree($node['children'], $key);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Rab*_*ire 5

您还可以更改函数中的参数以采用节点数组而不是单个节点。这稍微改变了递归,并防止需要传递密钥:

function pruneTree(&$nodes) {
    foreach ($nodes as $key => $node) {
        if (!$node['children'] && !$node['productCount']) {
            unset($nodes[$key]);
        } elseif (!empty($node['children'])) {
            pruneTree($nodes[$key]['children']);
            // This line checks if all the children have been pruned away:
            if (empty($nodes[$key]['children'])) {
                unset($nodes[$key]);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,添加了一项检查,以确保如果所有子节点都被修剪,则父(现在是叶)节点也会被修剪。

希望这可以帮助!


测试数据:

$data = array(
    6 => array(
        'id' => 6,
        'name' => 'computers',
        'productCount' => 0,
        'children' => array(
            91 => array(
                'id' => 91,
                'name' => 'notebook',
                'productCount' => 5,
                'children' => array()
            ),
            86 => array(
                'id' => 86,
                'name' => 'desktop',
                'productCount' => 0,
                'children' => array()
            )
        )
    )
);
Run Code Online (Sandbox Code Playgroud)

电话:

pruneTree($data);
echo '<pre>';
print_r($data);
echo '</pre>';
Run Code Online (Sandbox Code Playgroud)