小编sev*_*ers的帖子

递归并通过引用传递

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

[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 ( …
Run Code Online (Sandbox Code Playgroud)

php recursion pass-by-reference

7
推荐指数
2
解决办法
6064
查看次数

标签 统计

pass-by-reference ×1

php ×1

recursion ×1