代码之家  ›  专栏  ›  技术社区  ›  user1129107

如何在PHP数组中递归地向对象添加层

  •  -3
  • user1129107  · 技术社区  · 6 年前

    我在一个数组中有一组分层的对象,并希望使用PHP递归地循环它们,以添加相应的层。我该怎么做?

    注意:阵列的结构可能比图中显示的要深得多。

    Array
    (
        [0] => stdClass Object //should be tier 1
            (
                [name] => Object name
                [children] => Array
                    (
                        [0] => stdClass Object //should be tier 2
                            (
                                [name] => Object name
                                [children] => Array
                                    (
                                        [0] => stdClass Object //should be tier 3
                                            (
                                                [name] => Object name
                                                [children] => Array
                                                    (
                                                    )
                                            )
                                        [1] => stdClass Object //should be tier 3
                                            (
                                                [name] => Object name
                                                [children] => Array
                                                    (
                                                    )
                                            )
                                    )
                            )
                        [1] => stdClass Object //should be tier 2
                            (
                                [name] => Object name
                                [children] => Array
                                    (
                                        [0] => stdClass Object //should be tier 3
                                            (
                                                [name] => Object name
                                                [children] => Array
                                                    (
                                                    )
                                            )
                                    )
                            )
                    )
            )
        [1] => stdClass Object //should be tier 1
            (
                [name] => Object name
                [children] => Array
                    (
                    )
            )
    )
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   fubar    6 年前

    问题其实很简单。

    您需要迭代结构的每个级别,设置层,当您遇到具有子节点的节点时,递归调用相同的函数,但增加层值。

    我用过 json_decode 更轻松地创建 stdClass 对象,您可以在此处看到其工作: https://3v4l.org/oCJUV

    <?php
    
    function setTier(array $nodes, $tier = 1)
    {
        return array_map(function ($node) use ($tier) {
            $node->tier = $tier;
            $node->children = setTier($node->children, $tier + 1);
    
            return $node;
        }, $nodes);
    }
    
    $structure = json_decode('
        [{
            "name": "a",
            "children": [{
                "name": "c",
                "children": [{
                    "name": "e",
                    "children": [{
                        "name": "g",
                        "children": []
                    }]
                }, {
                    "name": "f",
                    "children": []
                }]
            }, {
                "name": "d",
                "children": [{
                    "name": "h",
                    "children": []
                }]
            }]
        }, {
            "name": "b",
            "children": []
        }]
    ');
    
    $structure = setTier($structure);
    
    print_r($structure); die;