3v4l.org

run code in 300+ PHP versions simultaneously
<?php $links = [ [ 1, 3], [ 3, 5], [ 3, 7], [ 3, 9], [ 1, 10], [10, 15], ]; $input = array_map(function ($link) { return ['id' => $link[1], 'parent' => $link[0]]; }, $links); function buildTreeMap($input, $child = 'id', $parent = 'parent') { $map = array(); foreach ($input as $node) { // init self if (!array_key_exists($node[$child], $map)) { $map[$node[$child]] = []; } // init parent if (!array_key_exists($node[$parent], $map)) { $map[$node[$parent]] = []; } // add to parent $map[$node[$parent]][$node[$child]] =& $map[$node[$child]]; } return $map; } $map = buildTreeMap($input); // the whole map print_r($map); // only from root 1 print_r($map[1]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RAO45
function name:  (null)
number of ops:  19
compiled vars:  !0 = $links, !1 = $input, !2 = $map
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   11     1        INIT_FCALL                                               'array_map'
          2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FRAO45%3A11%240'
   13     3        SEND_VAL                                                 ~4
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $5      
   11     6        ASSIGN                                                   !1, $5
   36     7        INIT_FCALL                                               'buildtreemap'
          8        SEND_VAR                                                 !1
          9        DO_FCALL                                      0  $7      
         10        ASSIGN                                                   !2, $7
   39    11        INIT_FCALL                                               'print_r'
         12        SEND_VAR                                                 !2
         13        DO_ICALL                                                 
   42    14        INIT_FCALL                                               'print_r'
         15        FETCH_DIM_R                                      ~10     !2, 1
         16        SEND_VAL                                                 ~10
         17        DO_ICALL                                                 
         18      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FRAO45%3A11%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RAO45
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $link
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
   12     1        FETCH_DIM_R                                      ~1      !0, 1
          2        INIT_ARRAY                                       ~2      ~1, 'id'
          3        FETCH_DIM_R                                      ~3      !0, 0
          4        ADD_ARRAY_ELEMENT                                ~2      ~3, 'parent'
          5      > RETURN                                                   ~2
   13     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FRAO45%3A11%240

Function buildtreemap:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 29
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 29
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 20
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 20
Branch analysis from position: 13
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
filename:       /in/RAO45
function name:  buildTreeMap
number of ops:  32
compiled vars:  !0 = $input, !1 = $child, !2 = $parent, !3 = $map, !4 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'id'
          2        RECV_INIT                                        !2      'parent'
   16     3        ASSIGN                                                   !3, <array>
   18     4      > FE_RESET_R                                       $6      !0, ->29
          5    > > FE_FETCH_R                                               $6, !4, ->29
   20     6    >   FETCH_DIM_R                                      ~7      !4, !1
          7        ARRAY_KEY_EXISTS                                 ~8      ~7, !3
          8        BOOL_NOT                                         ~9      ~8
          9      > JMPZ                                                     ~9, ->13
   21    10    >   FETCH_DIM_R                                      ~10     !4, !1
         11        ASSIGN_DIM                                               !3, ~10
         12        OP_DATA                                                  <array>
   25    13    >   FETCH_DIM_R                                      ~12     !4, !2
         14        ARRAY_KEY_EXISTS                                 ~13     ~12, !3
         15        BOOL_NOT                                         ~14     ~13
         16      > JMPZ                                                     ~14, ->20
   26    17    >   FETCH_DIM_R                                      ~15     !4, !2
         18        ASSIGN_DIM                                               !3, ~15
         19        OP_DATA                                                  <array>
   30    20    >   FETCH_DIM_R                                      ~17     !4, !2
         21        FETCH_DIM_R                                      ~19     !4, !1
         22        FETCH_DIM_R                                      ~21     !4, !1
         23        FETCH_DIM_W                                      $22     !3, ~21
         24        MAKE_REF                                         $23     $22
         25        FETCH_DIM_W                                      $18     !3, ~17
         26        FETCH_DIM_W                                      $20     $18, ~19
         27        ASSIGN_REF                                               $20, $23
   18    28      > JMP                                                      ->5
         29    >   FE_FREE                                                  $6
   33    30      > RETURN                                                   !3
   34    31*     > RETURN                                                   null

End of function buildtreemap

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.94 ms | 1402 KiB | 18 Q