PHP展平数组并添加深度键

时间:2014-09-26 17:56:00

标签: php multidimensional-array flatten

我有以下数组:

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

注意"深度" key - 这应该表明元素的原始数组有多深

自我调用/递归函数没问题

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

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