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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?