drt41563 2018-04-05 23:31
浏览 69
已采纳

如何以递归方式将层添加到数组PHP中的对象

I have a bunch of objects tiered in an array, and want to recursively loop through them with PHP to add their corresponding tiers. How can I do that?

NOTE: The structure of the array could go much deeper than shown.

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条回答 默认 最新

  • douxi8759 2018-04-06 00:12
    关注

    The problem is actually quite straight forward.

    You need to iterate over each level of the structure, setting the tier, and when you encounter a node with children, recursively call the same function, but increment the tier value.

    I used json_decode to more easily create the stdClass objects for the example, which you can see working here: 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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan