基于多维数组的子项构建 php 数组

Build php array based on children from multidimensional array

我有一个这样的 php 数组:

$arr = array(
    0 => array(
        "text" => "eventyrer",
        "children"=> array(
                4 => array(
                        "text" => "news",
                        "children"=> array(
                                1=> array("text"=>"a")
                            )
                    ),

                5 => array(
                        "text" => "nyheter",
                        "children"=> array(
                                1=> array("text"=>"b")
                            )
                    )
            ) 
    ),

    1 => array(
        "text" => "eventyrer2017",
        "children"=> array(
                6 => array(
                        "text" => "news",
                        "children"=> array(
                                1=> array("text"=>"c")
                            )
                    ),

                8 => array(
                        "text" => "nyheter",
                        "children"=> array(
                                1=> array("text"=>"d")
                            )
                    )
            ) 
    )

);

如何获得这样的输出:

$array = array(
    0 => "eventyrer/news/a",
    1 => "eventyrer/nyheter/b",
    2 => "eventyrer2017/news/c",
    4 => "eventyrer2017/nyheter/d",
)

这里我需要取 "text" 然后附加“/”,然后通过 "children" 取他们的文本。 来自子项的文本将与父项一起添加。

以下代码尝试递归方法并为 children 中的每个 child 附加 text 的每个段。这使得您的数据结构的无限深度成为可能。

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

    return $lst;
}

输出:

Array
(
    [0] => eventyrer/news/a
    [1] => eventyrer/nyheter/b
    [2] => eventyrer2017/news/c
    [3] => eventyrer2017/nyheter/d
)

我能够使用 3 个嵌套循环来完成此操作。

//initialize new array to hold newly generated strings
$new_array = [];

foreach($arr as &$value) {
    //loop through first set

    foreach($value['children'] as $child) {

        //loop through children, create a string that contains the first set of values "text", and the childrens "text"
        $string = "{$value['text']}/{$child['text']}";

        foreach($child['children'] as $child2) {

            //loop through each child of child1 and add a new string to the new_array containing `$string` + the child2.
            $new_array[] = $string .= "/{$child2['text']}"; 

        }
    }

}

输出:

Array ( [0] => eventyrer/news/a [1] => eventyrer/nyheter/b [2] => eventyrer2017/news/c [3] => eventyrer2017/nyheter/d )