3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $array = array(array('id' => '10', 'name' => 'aaa', 'nodes' =>array()), array('id' => '11', 'name' => 'bbb','nodes' =>array('id' => '21', 'name' => 'aaaaaa','nodes'=> array("11"=>"haha"))), array('id' => '12', 'name' => 'ccc', 'nodes' =>array()), array('id' => '13', 'name' => 'ddd', 'nodes' =>array('id' => '23', 'name' => 'cccccc','nodes'=> array())), array('id' => '14', 'name' => 'eee', 'nodes' =>array()) ); function FormatTagString($num){ $tag = "-"; $string=""; for ($i=0;$i<$num;$i++){ $string.=$tag; } return $string; } function GetTree($array,$level){ global $tree; foreach ($array as $k=>$v){ if (!is_array($v))$tree.=FormatTagString($level).$v."<br />"; else GetTree($v,$level+1); } return $tree; } $string = GetTree($array,1); print_r($string);
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
--10<br />--aaa<br />--11<br />--bbb<br />---21<br />---aaaaaa<br />----haha<br />--12<br />--ccc<br />--13<br />--ddd<br />---23<br />---cccccc<br />--14<br />--eee<br />