3v4l.org

run code in 300+ PHP versions simultaneously
<?php $list = [ [ 'name' => 'blah', 'path' => 'A' ], [ 'name' => 'blah', 'path' => 'AA' ], [ 'name' => 'blah', 'path' => 'AB' ], [ 'name' => 'blah', 'path' => 'B' ], [ 'name' => 'blah', 'path' => 'BA' ], [ 'name' => 'blah', 'path' => 'BAA' ], ]; $result = array_reduce($list, function ($a, $c) { $l = substr($c['path'], 0, 1); isset($a[$l]) ? $a[$l]['children'][] = $c : $a[$l] = [ 'name' => $c['name'], 'path' => $c['path'], 'children' => [] ]; return $a; }, []); // to remove the letter keys, uncomment the next line // $result = array_values($result); var_export($result);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fWsdc
function name:  (null)
number of ops:  12
compiled vars:  !0 = $list, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   30     1        INIT_FCALL                                               'array_reduce'
          2        SEND_VAR                                                 !0
          3        DECLARE_LAMBDA_FUNCTION                          ~3      [0]
   40     4        SEND_VAL                                                 ~3
          5        SEND_VAL                                                 <array>
   30     6        DO_ICALL                                         $4      
          7        ASSIGN                                                   !1, $4
   46     8        INIT_FCALL                                               'var_export'
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                                 
         11      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 17
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fWsdc
function name:  {closure}
number of ops:  28
compiled vars:  !0 = $a, !1 = $c, !2 = $l
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   31     2        INIT_FCALL                                               'substr'
          3        FETCH_DIM_R                                      ~3      !1, 'path'
          4        SEND_VAL                                                 ~3
          5        SEND_VAL                                                 0
          6        SEND_VAL                                                 1
          7        DO_ICALL                                         $4      
          8        ASSIGN                                                   !2, $4
   32     9        ISSET_ISEMPTY_DIM_OBJ                         0          !0, !2
         10      > JMPZ                                                     ~6, ->17
   33    11    >   FETCH_DIM_W                                      $7      !0, !2
         12        FETCH_DIM_W                                      $8      $7, 'children'
         13        ASSIGN_DIM                                       ~9      $8
         14        OP_DATA                                                  !1
         15        QM_ASSIGN                                        ~10     ~9
         16      > JMP                                                      ->25
   35    17    >   FETCH_DIM_R                                      ~12     !1, 'name'
         18        INIT_ARRAY                                       ~13     ~12, 'name'
   36    19        FETCH_DIM_R                                      ~14     !1, 'path'
         20        ADD_ARRAY_ELEMENT                                ~13     ~14, 'path'
   35    21        ADD_ARRAY_ELEMENT                                ~13     <array>, 'children'
   34    22        ASSIGN_DIM                                       ~11     !0, !2
   35    23        OP_DATA                                                  ~13
         24        QM_ASSIGN                                        ~10     ~11
         25    >   FREE                                                     ~10
   39    26      > RETURN                                                   !0
   40    27*     > RETURN                                                   null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.96 ms | 1012 KiB | 16 Q