3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ 0=>['id'=>1,'name'=>'父1','father'=>0], 1=>['id'=>2,'name'=>'父2','father'=>0], 2=>['id'=>3,'name'=>'父3','father'=>0], 3=>['id'=>4,'name'=>'儿1-1','father'=>1], 4=>['id'=>5,'name'=>'儿1-2','father'=>1], 5=>['id'=>6,'name'=>'儿1-3','father'=>1], 6=>['id'=>7,'name'=>'儿2-1','father'=>2], 7=>['id'=>8,'name'=>'儿2-1','father'=>2], 8=>['id'=>9,'name'=>'儿3-1','father'=>3], 9=>['id'=>10,'name'=>'儿3-1-1','father'=>9], 10=>['id'=>11,'name'=>'儿1-1-1','father'=>4], 11=>['id'=>12,'name'=>'儿2-1-1','father'=>7], ]; function generateTree($arr,$id,$step){ static $tree=[]; foreach($arr as $key=>$val) { if($val['father'] == $id) { $flg = str_repeat('└―',$step); $val[$val['id']] = $flg.$val['name']; $tree[$val['id']] = $val; generateTree($arr , $val['id'] ,$step+1); } } return $tree; } $tree = generateTree($arr,0,0); foreach ($tree as $val){ $new[] = $val['name'].'<br>'; } print_r($new);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 14
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/5Ye12
function name:  (null)
number of ops:  19
compiled vars:  !0 = $arr, !1 = $tree, !2 = $val, !3 = $new
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   29     1        INIT_FCALL                                               'generatetree'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 0
          4        SEND_VAL                                                 0
          5        DO_FCALL                                      0  $5      
          6        ASSIGN                                                   !1, $5
   30     7      > FE_RESET_R                                       $7      !1, ->14
          8    > > FE_FETCH_R                                               $7, !2, ->14
   31     9    >   FETCH_DIM_R                                      ~9      !2, 'name'
         10        CONCAT                                           ~10     ~9, '%3Cbr%3E'
         11        ASSIGN_DIM                                               !3
         12        OP_DATA                                                  ~10
   30    13      > JMP                                                      ->8
         14    >   FE_FREE                                                  $7
   34    15        INIT_FCALL                                               'print_r'
         16        SEND_VAR                                                 !3
         17        DO_ICALL                                                 
         18      > RETURN                                                   1

Function generatetree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 32
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 32
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 31
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 31
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/5Ye12
function name:  generateTree
number of ops:  35
compiled vars:  !0 = $arr, !1 = $id, !2 = $step, !3 = $tree, !4 = $val, !5 = $key, !6 = $flg
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   18     3        BIND_STATIC                                              !3
   19     4      > FE_RESET_R                                       $7      !0, ->32
          5    > > FE_FETCH_R                                       ~8      $7, !4, ->32
          6    >   ASSIGN                                                   !5, ~8
   20     7        FETCH_DIM_R                                      ~10     !4, 'father'
          8        IS_EQUAL                                                 !1, ~10
          9      > JMPZ                                                     ~11, ->31
   21    10    >   INIT_FCALL                                               'str_repeat'
         11        SEND_VAL                                                 '%E2%94%94%E2%80%95'
         12        SEND_VAR                                                 !2
         13        DO_ICALL                                         $12     
         14        ASSIGN                                                   !6, $12
   22    15        FETCH_DIM_R                                      ~14     !4, 'id'
         16        FETCH_DIM_R                                      ~16     !4, 'name'
         17        CONCAT                                           ~17     !6, ~16
         18        ASSIGN_DIM                                               !4, ~14
         19        OP_DATA                                                  ~17
   23    20        FETCH_DIM_R                                      ~18     !4, 'id'
         21        ASSIGN_DIM                                               !3, ~18
         22        OP_DATA                                                  !4
   24    23        INIT_FCALL_BY_NAME                                       'generateTree'
         24        SEND_VAR_EX                                              !0
         25        CHECK_FUNC_ARG                                           
         26        FETCH_DIM_FUNC_ARG                               $20     !4, 'id'
         27        SEND_FUNC_ARG                                            $20
         28        ADD                                              ~21     !2, 1
         29        SEND_VAL_EX                                              ~21
         30        DO_FCALL                                      0          
   19    31    > > JMP                                                      ->5
         32    >   FE_FREE                                                  $7
   27    33      > RETURN                                                   !3
   28    34*     > RETURN                                                   null

End of function generatetree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.7 ms | 1402 KiB | 18 Q