3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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", "children"=> array( 1=> array("text"=>"e") ) ) ) ) ) ) ); $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']}"; } } } var_export($new_array);
based on 97S5I
Output for 5.6.38 - 7.3.0rc3
array ( 0 => 'eventyrer/news/a', 1 => 'eventyrer/nyheter/b', 2 => 'eventyrer2017/news/c', 3 => 'eventyrer2017/nyheter/d', )