php - 如何在php中递归地展平树数组?

标签 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

最佳答案

合并内部展平输出。

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

关于php - 如何在php中递归地展平树数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41562516/

相关文章:

php - 比较数据库中的时间和使用 date() 函数检索的当前时间

java - 将 ArrayList 转换为数组(有点不同)

java - 递归语句中声明的变量是创建新变量还是刷新原始变量?

list - 在 Haskell 中将元素插入列表中给定的索引

php - 特定页面的 apache 重写规则问题

php - 将两个模型合并为一个数据库调用 - laravel

php - 可以使用PHP保护音频文件吗?

c# - 以互操作 C++ 的数组形式返回数据的更快方法

ios - '-[__NSCFArray objectAtIndex :]: index (1) beyond bounds (1)' after item added to array

javascript - Promise 的奇怪无限递归行为