【问题标题】:How to create a path based on array with multiple children如何基于具有多个孩子的数组创建路径
【发布时间】:2019-10-21 17:37:52
【问题描述】:

所以,基本上我要做的是获取数组子元素的组合,例如我得到了数组:

[
"name" => "Item1", 
"children" => 
        [
        "name" => "Item2",
        "children" => [
                ["name" => "Item3"],
                ["name" => "Item4"]
                ]
        ],
        ["name" => "Item5"]
];

我尝试使用我在 stackoverflow 上获得的一些功能,但我只能让它同时使用所有这些功能,我只是得到了

[
"Item4" => "Item1/Item2/Item4",
"Item5" => "Item1/Item5"
];

输出应该是

[
"Item1" => "Item1",
"Item2" => "Item1/Item2",
"Item3" => "Item1/Item2/Item3"
"Item4" => "Item1/Item2/Item4"
"Item5" => "Item1/Item5"
];

如被问及,我之前使用的函数:

function flatten($arr) {
    $lst = [];
    /* Iterate over each item at the current level */
    foreach ($arr as $key => $item) {
        /* Get the "prefix" of the URL */
        $prefix = $item['slug'];
        /* Check if it has children */
        if (array_key_exists('children', $item) and sizeof($item['children'])) {
            /* Get the suffixes recursively */
            $suffixes = flatten($item['children']);
            /* Add it to the current prefix */
            foreach($suffixes as $suffix) {
                $url = $prefix . '/' . $suffix;
                $lst[$item['id']] = $url;
            }
        } else {
            /* If there are no children, just add the
             * current prefix to the list */
            $lst[$item['id']] = $prefix;
        }
    }

    return $lst;
}

【问题讨论】:

    标签: php arrays laravel multidimensional-array


    【解决方案1】:

    由于数据级别不匹配,我不得不修复数据。其余代码是新的,因为我从您现有的代码中发现了很多错误。

    代码中的注释...

    $data = [
        "name" => "Item1",
        "children" =>
        [[
            "name" => "Item2",
            "children" =>[
            ["name" => "Item3"],
            ["name" => "Item4"]]
        ],
        ["name" => "Item5"]]
    ];
    
    print_r(flatten($data));
    
    function flatten($arr, $pathSoFar = '') {
        $lst = [];
        $path = $pathSoFar."/";
        foreach ( $arr as $key => $value )  {
            if ( $key === 'name' )   {
                // Add name of current level onto path
                $path .= $value;
                $lst[$value] = $path;
            }
            else if ( $key === 'children' )  {
                //Process child elements recursively and add into current array
                $lst = array_merge($lst, flatten($value, $path));
            }
            else    {
                // This is for sub-elements which probably are (for example) 0, 1
                // (removing trailing / to stop multiples)
                $lst = array_merge($lst, flatten($value, rtrim($path,"/")));
            }
        }
        return $lst;
    }
    

    【讨论】:

    • 谢谢,我可以使用您的代码,但我仍然不明白为什么它返回每种可能性而不是它们的总和
    • 您可以单步调试代码或添加调试数据以显示它的工作原理。
    猜你喜欢
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    相关资源
    最近更新 更多