3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $arrs = array( array('id'=>1,'name'=>'root','pid'=>0), array('id'=>2,'name'=>'root1','pid'=>0), array('id'=>3,'name'=>'level1','pid'=>1), array('id'=>4,'name'=>'level11','pid'=>1), array('id'=>5,'name'=>'leve21','pid'=>2), array('id'=>6,'name'=>'level2','pid'=>2) ); class digui{ function index($id,$arr){ echo $this->doarr($id,$arr); } private function doarr($id,$arr ){ foreach($arr as $key => $val){ if($id==$val['pid']){ if($val['pid']!=0) { $this->doarr($val['pid'],$arr); return $val['name']; } else return $val['name']; } } } } $d = new digui; //$d->index(3,$arrs); function test($a=0,&$result=array()){ $a++; if ($a<10) { $result[]=$a; test($a,$result); } echo $a; print_r($result); return $result; } function test1($a=0){ static $result=array(); $a++; if ($a<10) { $result[]=$a; test1($a); print_r($result); } return $result; } $arrs = array( array('id'=>1,'name'=>'root','pid'=>0), array('id'=>2,'name'=>'root1','pid'=>0), array('id'=>3,'name'=>'level1','pid'=>1), array('id'=>4,'name'=>'level11','pid'=>1), array('id'=>5,'name'=>'leve21','pid'=>2), array('id'=>6,'name'=>'level2','pid'=>2) ); function getree($data,$pid){ $tree = array(); foreach($data as $key=>$val){ if($pid == $val['pid']){ $val['pid'] = getree($data,$val['id']); $tree[] = $val; } } return $tree; } function getree1($data,$pid){ $tree = array(); foreach($data as $key=>$val){ if($pid==$val['pid']){ $val['pid'] = getree1($data,$val['id']); $tree[] = $val; } return $tree; } } $res = getree1($arrs,0); $rows = array( array( 'id' => 1, 'name' => 'dev', 'parentid' => 0 ), array( 'id' => 2, 'name' => 'php', 'parentid' => 1 ), array( 'id' => 3, 'name' => 'smarty', 'parentid' => 2 ), array( 'id' => 4, 'name' => 'life', 'parentid' => 0 ), array( 'id' => 5, 'name' => 'pdo', 'parentid' => 2 ), array( 'id' => 6, 'name' => 'pdo-mysql', 'parentid' => 5 ), array( 'id' => 7, 'name' => 'java', 'parentid' => 1 ) ); // 72648 // 84072 function buildtree($rows,$root_id){ $childs = findchild($rows,0); if(empty($childs) ) return null; foreach($childs as $k=>$v){ $rescurtree = buildtree($rows,$v['id']); if(!empty($rescurtree)){ $rescurtree[$k]['childs'] = $rescurtree; } } return $rescurtree; } function findchild($rows,$id){ $childs = array(); foreach($rows as $k=>$v){ if($v['parentid']==$id) $childs[] = $v; } return $childs; } buildtree($rows,0);
based on nhX8B
Output for 7.0.0 - 7.0.11, 7.1.0, 7.1.6 - 7.1.7, 7.1.20 - 7.3.0rc4
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 20480 bytes) in /in/MbklT on line 165
Process exited with code 255.
Output for 7.1.5, 7.1.10

Process exited with code 137.
Output for 7.0.20
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 262144 bytes) in /in/MbklT on line 147
Process exited with code 255.
Output for 5.6.0 - 5.6.26
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 72 bytes) in /in/MbklT on line 165
Process exited with code 255.