3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for 7.0.20, 8.2.18, 8.3.6
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 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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 8.3.0 - 8.3.4
Fatal error: Out of memory (allocated 29360128 bytes) (tried to allocate 262144 bytes) in /in/MbklT on line 147 mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 8.2.0 - 8.2.17
Fatal error: Out of memory (allocated 31457280 bytes) (tried to allocate 262144 bytes) in /in/MbklT on line 147 mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 7.0.0 - 7.0.11, 7.1.0, 7.1.6 - 7.1.7, 7.1.20 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.1, 8.1.28
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 8.1.2 - 8.1.27
Fatal error: Out of memory (allocated 31457280) (tried to allocate 20480 bytes) in /in/MbklT on line 165 mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 7.3.32 - 7.3.33, 8.0.13
Fatal error: Out of memory (allocated 130023424) (tried to allocate 262144 bytes) in /in/MbklT on line 147 mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 7.1.5, 7.1.10

Process exited with code 137.
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.

preferences:
162.78 ms | 401 KiB | 219 Q