Build php array based on children from multidimensional array

I have a php array like this:

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

);

How can I get output like this:

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

Here I need to take the "text" and then append "/" then go through the "children" to take their text. The text from the children will be added with the parent one.

728x90

2 Answers Build php array based on children from multidimensional array

The following code attempts a recursive approach and appends each segment of text for each child in children. This makes it possible to have an infinite-ish depth of your data structure.

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

Output:

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

6 months ago

I was able to do this with 3 nested loops.

//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']}"; 

        }
    }

}

Output:

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

6 months ago