【问题标题】:How to recursively add tier to objects in an array PHP如何递归地向数组 PHP 中的对象添加层
【发布时间】:2018-04-05 23:31:48
【问题描述】:

我有一堆分层在一个数组中的对象,并且想用 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
                (
                )
        )
)

【问题讨论】:

  • 你试过什么?你具体在哪里遇到困难。发布您的代码。
  • 我放弃了要求 OP 发布他们自己的努力,因为我很无聊,想看看这样做有多么容易。
  • @fubar 我整天都被困在这个问题上。我尝试了几次失败尝试的迭代,因此随机选择一个示例进行分解似乎适得其反。您的解决方案似乎可以通过您的 jsfiddle 工作...感谢您的宝贵时间并希望它成功!
  • 超级!有用。谢谢您的帮助。我将您的答案标记为正确。

标签: php recursion php-7


【解决方案1】:

这个问题其实很简单。

你需要遍历结构的每一层,设置层,当遇到有子节点时,递归调用相同的函数,但增加层值。

我使用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;

【讨论】:

    猜你喜欢
    • 2019-10-21
    • 2020-07-06
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 2019-12-31
    • 2013-09-05
    相关资源
    最近更新 更多