如何在php中递归地压缩树数组?

时间:2017-01-10 06:33:39

标签: php arrays recursion

我有一个嵌套数组,我想以递归方式展平。我的功能适用于某些情况,而其他情况则失败。我无法理解什么是错的。

嵌套数组示例:

Array
(
    [0] => Array
        (
            [id] => 6
            [login] => Breanne
            [level] => Subdealer
            [type] => admin
            [supervisor_id] => 7
            [name] => Dominique Berge
            [active] => 0
            [pending] => 0
            [waiting] => 0
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 10
                            [login] => Priscilla
                            [level] => Subdealer
                            [type] => employee
                            [supervisor_id] => 6
                            [name] => Naomie Hudson
                            [active] => 0
                            [pending] => 0
                            [waiting] => 0
                        )

                )

        )

    [1] => Array
        (
            [id] => 15
            [login] => Myrtis
            [level] => Dealer
            [type] => manager
            [supervisor_id] => 7
            [name] => Dr. Neha Harris
            [active] => 0
            [pending] => 0
            [waiting] => 0
        )

    [2] => Array
        (
            [id] => 18
            [login] => Leon
            [level] => Dealer
            [type] => employee
            [supervisor_id] => 7
            [name] => Felipa Pacocha
            [active] => 0
            [pending] => 0
            [waiting] => 0
        )

)

平坦的例子:

Array
(
    [0] => Array
        (
            [id] => 10
            [login] => Priscilla
            [level] => Subdealer
            [type] => employee
            [supervisor_id] => 6
            [name] => Naomie Hudson
            [active] => 0
            [pending] => 0
            [waiting] => 0
        )

    [1] => Array
        (
            [id] => 6
            [login] => Breanne
            [level] => Subdealer
            [type] => admin
            [supervisor_id] => 7
            [name] => Dominique Berge
            [active] => 0
            [pending] => 0
            [waiting] => 0
        )

    [2] => Array
        (
            [id] => 15
            [login] => Myrtis
            [level] => Dealer
            [type] => manager
            [supervisor_id] => 7
            [name] => Dr. Neha Harris
            [active] => 0
            [pending] => 0
            [waiting] => 0
        )

    [3] => Array
        (
            [id] => 18
            [login] => Leon
            [level] => Dealer
            [type] => employee
            [supervisor_id] => 7
            [name] => Felipa Pacocha
            [active] => 0
            [pending] => 0
            [waiting] => 0
        )

)

我的功能:

function flatten($element)
{
    $flatArray = array();
    if (count($element) == 1 && !array_key_exists('child', $element) && !is_array($element)) {
        $flatArray[] = $element;
    }
    foreach ($element as $key => $node) {
        if (array_key_exists('child', $node)) {
            $flatArray =  flatten($node['child']);
            unset($node['child']);
            $flatArray[] = $node;
        } else {
            $flatArray[] = $node;
        }
    }


    return $flatArray;
}

失败的案例太长了,我把它放在一个链接为https://drive.google.com/file/d/0B5A_yGNzXeg5UG11YWJDXzlMQVk/view?usp=sharing

的文件中

2 个答案:

答案 0 :(得分:3)

合并内部展平输出。

function flatten($element)
{
    $flatArray = array();
    foreach ($element as $key => $node) {
        if (array_key_exists('child', $node)) {
            $flatArray = array_merge($flatArray,flatten($node['child']));
            unset($node['child']);
            $flatArray[] = $node;
        } else {
            $flatArray[] = $node;
        }
    }


    return $flatArray;
}

答案 1 :(得分:-1)

function flatten_array($items, array $flattened = []) {
    foreach ($items as $key => $value) {
        if (is_array($value)) {
            $flattened = flatten_array($value, $flattened);
            continue;
        }

        $flattened[$key] = $value;
    }

    return $flattened;
}