PHP flatten array and add depth key

笑着哭i 提交于 2019-12-11 09:45:42

问题


I have the following array:

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

Any ideas?


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


来源:https://stackoverflow.com/questions/26065557/php-flatten-array-and-add-depth-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!