3v4l.org

run code in 300+ PHP versions simultaneously
<?php $rows = [ ['name' => 'blah', 'path' => 'A'], ['name' => 'blah', 'path' => 'AA'], ['name' => 'blah', 'path' => 'AB'], ['name' => 'blah', 'path' => 'C'], ['name' => 'blah', 'path' => 'D'], ['name' => 'blah', 'path' => 'B'], ['name' => 'blah', 'path' => 'BA'], ['name' => 'blah', 'path' => 'BAA'], ['name' => 'blah', 'path' => 'CA'], ]; function rowsToTree(&$array, $parent = '') { $children = []; foreach ($array as $i => &$row) { if ($parent === substr($row['path'], 0, -1)) { $found = rowsToTree($array, $row['path']); if ($found) { $row['children'] = $found; if ($parent) { unset($array[$i]); } } if ($parent) { $children[] = $row; unset($array[$i]); } } } return $children; } rowsToTree($rows); var_export($rows);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QS6Eu
function name:  (null)
number of ops:  8
compiled vars:  !0 = $rows
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   34     1        INIT_FCALL                                               'rowstotree'
          2        SEND_REF                                                 !0
          3        DO_FCALL                                      0          
   35     4        INIT_FCALL                                               'var_export'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function rowstotree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 4, Position 2 = 31
Branch analysis from position: 4
2 jumps found. (Code = 126) Position 1 = 5, Position 2 = 31
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 30
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 26
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 26
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 30
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 30
Branch analysis from position: 26
Branch analysis from position: 26
Branch analysis from position: 30
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
filename:       /in/QS6Eu
function name:  rowsToTree
number of ops:  34
compiled vars:  !0 = $array, !1 = $parent, !2 = $children, !3 = $row, !4 = $i, !5 = $found
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      ''
   16     2        ASSIGN                                                   !2, <array>
   17     3      > FE_RESET_RW                                      $7      !0, ->31
          4    > > FE_FETCH_RW                                      ~8      $7, !3, ->31
          5    >   ASSIGN                                                   !4, ~8
   18     6        INIT_FCALL                                               'substr'
          7        FETCH_DIM_R                                      ~10     !3, 'path'
          8        SEND_VAL                                                 ~10
          9        SEND_VAL                                                 0
         10        SEND_VAL                                                 -1
         11        DO_ICALL                                         $11     
         12        IS_IDENTICAL                                             !1, $11
         13      > JMPZ                                                     ~12, ->30
   19    14    >   INIT_FCALL_BY_NAME                                       'rowsToTree'
         15        SEND_VAR_EX                                              !0
         16        CHECK_FUNC_ARG                                           
         17        FETCH_DIM_FUNC_ARG                               $13     !3, 'path'
         18        SEND_FUNC_ARG                                            $13
         19        DO_FCALL                                      0  $14     
         20        ASSIGN                                                   !5, $14
   20    21      > JMPZ                                                     !5, ->26
   21    22    >   ASSIGN_DIM                                               !3, 'children'
         23        OP_DATA                                                  !5
   22    24      > JMPZ                                                     !1, ->26
   23    25    >   UNSET_DIM                                                !0, !4
   26    26    > > JMPZ                                                     !1, ->30
   27    27    >   ASSIGN_DIM                                               !2
         28        OP_DATA                                                  !3
   28    29        UNSET_DIM                                                !0, !4
   17    30    > > JMP                                                      ->4
         31    >   FE_FREE                                                  $7
   32    32      > RETURN                                                   !2
   33    33*     > RETURN                                                   null

End of function rowstotree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.57 ms | 1018 KiB | 16 Q