3v4l.org

run code in 300+ PHP versions simultaneously
<?php function treeArray($data) { $result = array(); $p = array(); foreach($data as $val) { if($val['pid'] == 0) { $i = count($result); $result[$i] = isset($p[$val['id']])? array_merge($val,$p[$val['id']]) : $val; $p[$val['id']] = & $result[$i]; } } return $result; } $data = array(array('id'=>1,'pid'=>'0','name'=>'11111'), array('id'=>2,'pid'=>'1','name'=>'22222'), array('id'=>3,'pid'=>'0','name'=>'33333'), array('id'=>4,'pid'=>'3','name'=>'44444'), array('id'=>5,'pid'=>'4','name'=>'55555'), array('id'=>6,'pid'=>'1','name'=>'66666')); print_r(treeArray($data));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FMZAL
function name:  (null)
number of ops:  8
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   ASSIGN                                                   !0, <array>
   24     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'treearray'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function treearray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 30
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 30
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 29
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 21
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 29
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
filename:       /in/FMZAL
function name:  treeArray
number of ops:  33
compiled vars:  !0 = $data, !1 = $result, !2 = $p, !3 = $val, !4 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, <array>
    5     2        ASSIGN                                                   !2, <array>
    6     3      > FE_RESET_R                                       $7      !0, ->30
          4    > > FE_FETCH_R                                               $7, !3, ->30
    8     5    >   FETCH_DIM_R                                      ~8      !3, 'pid'
          6        IS_EQUAL                                                 ~8, 0
          7      > JMPZ                                                     ~9, ->29
   10     8    >   COUNT                                            ~10     !1
          9        ASSIGN                                                   !4, ~10
   11    10        FETCH_DIM_R                                      ~13     !3, 'id'
         11        ISSET_ISEMPTY_DIM_OBJ                         0          !2, ~13
         12      > JMPZ                                                     ~14, ->21
         13    >   INIT_FCALL                                               'array_merge'
         14        SEND_VAR                                                 !3
         15        FETCH_DIM_R                                      ~15     !3, 'id'
         16        FETCH_DIM_R                                      ~16     !2, ~15
         17        SEND_VAL                                                 ~16
         18        DO_ICALL                                         $17     
         19        QM_ASSIGN                                        ~18     $17
         20      > JMP                                                      ->22
         21    >   QM_ASSIGN                                        ~18     !3
         22    >   ASSIGN_DIM                                               !1, !4
         23        OP_DATA                                                  ~18
   12    24        FETCH_DIM_R                                      ~19     !3, 'id'
         25        FETCH_DIM_W                                      $21     !1, !4
         26        MAKE_REF                                         $22     $21
         27        FETCH_DIM_W                                      $20     !2, ~19
         28        ASSIGN_REF                                               $20, $22
    6    29    > > JMP                                                      ->4
         30    >   FE_FREE                                                  $7
   16    31      > RETURN                                                   !1
   17    32*     > RETURN                                                   null

End of function treearray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.21 ms | 1402 KiB | 18 Q