【问题标题】:PHP flatten array and add depth keyPHP展平数组并添加深度键
【发布时间】:2014-11-21 19:39:09
【问题描述】:

我有以下数组:

Array
(
    [0] => Array
        (
            [id] => 2
            [title] => Root 2
            [description] => 
            [site_id] => 1
            [parent_id] => 0
            [created_at] => 
            [updated_at] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [title] => Child 2
                            [description] => 
                            [site_id] => 1
                            [parent_id] => 2
                            [created_at] => 
                            [updated_at] => 
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 6
                                            [title] => Child 4
                                            [description] => 
                                            [site_id] => 1
                                            [parent_id] => 4
                                            [created_at] => 
                                            [updated_at] => 
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 7
            [title] => Root 3
            [description] => 
            [site_id] => 1
            [parent_id] => 0
            [created_at] => 
            [updated_at] => 
        )

)

我想将其展平为如下所示:

Array
(
    [0] => Array
        (
            [id] => 2
            [title] => Root 2
            [description] => 
            [site_id] => 1
            [parent_id] => 0
            [created_at] => 
            [updated_at] =>
            [depth] => 0
        )
    [1] => Array
        (
            [id] => 4
            [title] => Child 2
            [description] => 
            [site_id] => 1
            [parent_id] => 2
            [created_at] => 
            [updated_at] =>
            [depth] => 1
        )
    [2] => Array
        (
            [id] => 6
            [title] => Child 4
            [description] => 
            [site_id] => 1
            [parent_id] => 4
            [created_at] => 
            [updated_at] => 
            [depth] => 2
        )
    [3] => Array
        (
            [id] => 7
            [title] => Root 3
            [description] => 
            [site_id] => 1
            [parent_id] => 0
            [created_at] => 
            [updated_at] => 
            [depth] => 0
        )
)

注意“深度”键 - 这应该表明元素在原始数组中的深度

自调用/递归函数没问题

有什么想法吗?

【问题讨论】:

  • 你有没有做过任何可以展示的尝试?你哪里有问题?

标签: php multidimensional-array flatten


【解决方案1】:
function flatten($elements, $depth) {
    $result = array();

    foreach ($elements as $element) {
        $element['depth'] = $depth;

        if (isset($element['children'])) {
            $children = $element['children'];
            unset($element['children']);
        } else {
            $children = null;
        }

        $result[] = $element;

        if (isset($children)) {
            $result = array_merge($result, flatten($children, $depth + 1));
        }
    }

    return $result;
}

//call it like this
flatten($tree, 0);

【讨论】:

  • 谢谢,这很有帮助 - 与我所拥有的非常相似,但我对深度感到困惑......再次感谢
猜你喜欢
  • 2013-06-13
  • 2016-05-18
  • 2022-11-20
  • 2018-11-27
  • 2010-09-30
  • 2013-09-12
  • 2020-07-12
  • 1970-01-01
  • 2021-09-08
相关资源
最近更新 更多