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);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MbklT
function name:  (null)
number of ops:  16
compiled vars:  !0 = $arrs, !1 = $d, !2 = $res, !3 = $rows
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   35     1        NEW                                              $5      'digui'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $5
   62     4        ASSIGN                                                   !0, <array>
   99     5        INIT_FCALL                                               'getree1'
          6        SEND_VAR                                                 !0
          7        SEND_VAL                                                 0
          8        DO_FCALL                                      0  $9      
          9        ASSIGN                                                   !2, $9
  103    10        ASSIGN                                                   !3, <array>
  171    11        INIT_FCALL                                               'buildtree'
         12        SEND_VAR                                                 !3
         13        SEND_VAL                                                 0
         14        DO_FCALL                                      0          
         15      > RETURN                                                   1

Function test:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/MbklT
function name:  test
number of ops:  17
compiled vars:  !0 = $a, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV_INIT                                        !0      0
          1        RECV_INIT                                        !1      <array>
   39     2        PRE_INC                                                  !0
   40     3        IS_SMALLER                                               !0, 10
          4      > JMPZ                                                     ~3, ->11
   41     5    >   ASSIGN_DIM                                               !1
          6        OP_DATA                                                  !0
   42     7        INIT_FCALL_BY_NAME                                       'test'
          8        SEND_VAR_EX                                              !0
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0          
   44    11    >   ECHO                                                     !0
   45    12        INIT_FCALL                                               'print_r'
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                                 
   46    15      > RETURN                                                   !1
   48    16*     > RETURN                                                   null

End of function test

Function test1:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/MbklT
function name:  test1
number of ops:  15
compiled vars:  !0 = $a, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV_INIT                                        !0      0
   50     1        BIND_STATIC                                              !1
   52     2        PRE_INC                                                  !0
   53     3        IS_SMALLER                                               !0, 10
          4      > JMPZ                                                     ~3, ->13
   54     5    >   ASSIGN_DIM                                               !1
          6        OP_DATA                                                  !0
   55     7        INIT_FCALL_BY_NAME                                       'test1'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0          
   56    10        INIT_FCALL                                               'print_r'
         11        SEND_VAR                                                 !1
         12        DO_ICALL                                                 
   59    13    > > RETURN                                                   !1
   60    14*     > RETURN                                                   null

End of function test1

Function getree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 20
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 20
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 19
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 19
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/MbklT
function name:  getree
number of ops:  23
compiled vars:  !0 = $data, !1 = $pid, !2 = $tree, !3 = $val, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   73     2        ASSIGN                                                   !2, <array>
   74     3      > FE_RESET_R                                       $6      !0, ->20
          4    > > FE_FETCH_R                                       ~7      $6, !3, ->20
          5    >   ASSIGN                                                   !4, ~7
   75     6        FETCH_DIM_R                                      ~9      !3, 'pid'
          7        IS_EQUAL                                                 !1, ~9
          8      > JMPZ                                                     ~10, ->19
   76     9    >   INIT_FCALL_BY_NAME                                       'getree'
         10        SEND_VAR_EX                                              !0
         11        CHECK_FUNC_ARG                                           
         12        FETCH_DIM_FUNC_ARG                               $12     !3, 'id'
         13        SEND_FUNC_ARG                                            $12
         14        DO_FCALL                                      0  $13     
         15        ASSIGN_DIM                                               !3, 'pid'
         16        OP_DATA                                                  $13
   77    17        ASSIGN_DIM                                               !2
         18        OP_DATA                                                  !3
   74    19    > > JMP                                                      ->4
         20    >   FE_FREE                                                  $6
   83    21      > RETURN                                                   !2
   84    22*     > RETURN                                                   null

End of function getree

Function getree1:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 22
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 22
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 19
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/MbklT
function name:  getree1
number of ops:  24
compiled vars:  !0 = $data, !1 = $pid, !2 = $tree, !3 = $val, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   88     2        ASSIGN                                                   !2, <array>
   89     3      > FE_RESET_R                                       $6      !0, ->22
          4    > > FE_FETCH_R                                       ~7      $6, !3, ->22
          5    >   ASSIGN                                                   !4, ~7
   90     6        FETCH_DIM_R                                      ~9      !3, 'pid'
          7        IS_EQUAL                                                 !1, ~9
          8      > JMPZ                                                     ~10, ->19
   92     9    >   INIT_FCALL_BY_NAME                                       'getree1'
         10        SEND_VAR_EX                                              !0
         11        CHECK_FUNC_ARG                                           
         12        FETCH_DIM_FUNC_ARG                               $12     !3, 'id'
         13        SEND_FUNC_ARG                                            $12
         14        DO_FCALL                                      0  $13     
         15        ASSIGN_DIM                                               !3, 'pid'
         16        OP_DATA                                                  $13
   93    17        ASSIGN_DIM                                               !2
         18        OP_DATA                                                  !3
   95    19    >   FE_FREE                                                  $6
         20      > RETURN                                                   !2
   89    21*       JMP                                                      ->4
         22    >   FE_FREE                                                  $6
   98    23      > RETURN                                                   null

End of function getree1

Function buildtree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 28
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 28
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 27
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 27
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/MbklT
function name:  buildtree
number of ops:  31
compiled vars:  !0 = $rows, !1 = $root_id, !2 = $childs, !3 = $v, !4 = $k, !5 = $rescurtree
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  144     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  147     2        INIT_FCALL_BY_NAME                                       'findchild'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAL_EX                                              0
          5        DO_FCALL                                      0  $6      
          6        ASSIGN                                                   !2, $6
  148     7        ISSET_ISEMPTY_CV                                         !2
          8      > JMPZ                                                     ~8, ->10
          9    > > RETURN                                                   null
  150    10    > > FE_RESET_R                                       $9      !2, ->28
         11    > > FE_FETCH_R                                       ~10     $9, !3, ->28
         12    >   ASSIGN                                                   !4, ~10
  152    13        INIT_FCALL_BY_NAME                                       'buildtree'
         14        SEND_VAR_EX                                              !0
         15        CHECK_FUNC_ARG                                           
         16        FETCH_DIM_FUNC_ARG                               $12     !3, 'id'
         17        SEND_FUNC_ARG                                            $12
         18        DO_FCALL                                      0  $13     
         19        ASSIGN                                                   !5, $13
  153    20        ISSET_ISEMPTY_CV                                 ~15     !5
         21        BOOL_NOT                                         ~16     ~15
         22      > JMPZ                                                     ~16, ->27
  154    23    >   QM_ASSIGN                                        ~19     !5
         24        FETCH_DIM_W                                      $17     !5, !4
         25        ASSIGN_DIM                                               $17, 'childs'
         26        OP_DATA                                                  ~19
  150    27    > > JMP                                                      ->11
         28    >   FE_FREE                                                  $9
  157    29      > RETURN                                                   !5
  158    30*     > RETURN                                                   null

End of function buildtree

Function findchild:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 11
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/MbklT
function name:  findchild
number of ops:  15
compiled vars:  !0 = $rows, !1 = $id, !2 = $childs, !3 = $v, !4 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  160     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  161     2        ASSIGN                                                   !2, <array>
  162     3      > FE_RESET_R                                       $6      !0, ->12
          4    > > FE_FETCH_R                                       ~7      $6, !3, ->12
          5    >   ASSIGN                                                   !4, ~7
  164     6        FETCH_DIM_R                                      ~9      !3, 'parentid'
          7        IS_EQUAL                                                 !1, ~9
          8      > JMPZ                                                     ~10, ->11
  165     9    >   ASSIGN_DIM                                               !2
         10        OP_DATA                                                  !3
  162    11    > > JMP                                                      ->4
         12    >   FE_FREE                                                  $6
  168    13      > RETURN                                                   !2
  170    14*     > RETURN                                                   null

End of function findchild

Class digui:
Function index:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MbklT
function name:  index
number of ops:  8
compiled vars:  !0 = $id, !1 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   16     2        INIT_METHOD_CALL                                         'doarr'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0  $2      
          6        ECHO                                                     $2
   17     7      > RETURN                                                   null

End of function index

Function doarr:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 24
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 24
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 23
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 20
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/MbklT
function name:  doarr
number of ops:  26
compiled vars:  !0 = $id, !1 = $arr, !2 = $val, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   21     2      > FE_RESET_R                                       $4      !1, ->24
          3    > > FE_FETCH_R                                       ~5      $4, !2, ->24
          4    >   ASSIGN                                                   !3, ~5
   22     5        FETCH_DIM_R                                      ~7      !2, 'pid'
          6        IS_EQUAL                                                 !0, ~7
          7      > JMPZ                                                     ~8, ->23
   23     8    >   FETCH_DIM_R                                      ~9      !2, 'pid'
          9        IS_NOT_EQUAL                                             ~9, 0
         10      > JMPZ                                                     ~10, ->20
   25    11    >   INIT_METHOD_CALL                                         'doarr'
         12        FETCH_DIM_R                                      ~11     !2, 'pid'
         13        SEND_VAL                                                 ~11
         14        SEND_VAR                                                 !1
         15        DO_FCALL                                      0          
   26    16        FETCH_DIM_R                                      ~13     !2, 'name'
         17        FE_FREE                                                  $4
         18      > RETURN                                                   ~13
         19*       JMP                                                      ->23
   28    20    >   FETCH_DIM_R                                      ~14     !2, 'name'
         21        FE_FREE                                                  $4
         22      > RETURN                                                   ~14
   21    23    > > JMP                                                      ->3
         24    >   FE_FREE                                                  $4
   32    25      > RETURN                                                   null

End of function doarr

End of class digui.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.37 ms | 1410 KiB | 17 Q