PHP展平数组并添加深度键 [英] PHP flatten array and add depth key

查看:70
本文介绍了PHP展平数组并添加深度键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

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] => 
        )

)

我想将其展平为以下内容:

I would like to flatten it to something like the following:

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
        )
)

请注意深度"键-这应指示元素在原始数组中的深度

Note the "depth" key - this should indicate how deep in original array the element was

自调用/递归函数没问题

Self calling/recursive functions are no problem

有什么想法吗?

推荐答案

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);

这篇关于PHP展平数组并添加深度键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆