3v4l.org

run code in 300+ PHP versions simultaneously
<?php function treeify(array $input): array { $result = []; $path = []; foreach ($input as &$entry) { $entry['children'] = []; $depth = $entry['depth'] - 1; $path = \array_slice($path, 0, $depth); $path[] = &$entry; 0 === $depth ? $result[] = &$entry : $path[$depth - 1]['children'][] = &$entry; } return $result; } print_r(treeify([ ['name' => 'Root_1', 'depth' => 1], ['name' => 'Children', 'depth' => 2], ['name' => 'Children', 'depth' => 2], ['name' => 'Children', 'depth' => 2], ['name' => 'Children', 'depth' => 3], ['name' => 'Children', 'depth' => 3], ['name' => 'Children', 'depth' => 3], ['name' => 'Children', 'depth' => 4], ['name' => 'Children', 'depth' => 2], ['name' => 'Root_2', 'depth'=> 1], ['name' => 'Children', 'depth' => 2], ['name' => 'Children', 'depth' => 2], ['name' => 'Children', 'depth' => 2], ['name' => 'Children', 'depth' => 3], ['name' => 'Root_3', 'depth' => 1], ['name' => 'Root_4', 'depth' => 1], ['name' => 'Children', 'depth' => 2], ]));

Abusive script

This script was stopped while abusing our resources

Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1YkvY
function name:  (null)
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   INIT_FCALL                                               'print_r'
          1        INIT_FCALL                                               'treeify'
   24     2        SEND_VAL                                                 <array>
   23     3        DO_FCALL                                      0  $0      
   24     4        SEND_VAR                                                 $0
   23     5        DO_ICALL                                                 
   41     6      > RETURN                                                   1

Function treeify:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 4, Position 2 = 32
Branch analysis from position: 4
2 jumps found. (Code = 126) Position 1 = 5, Position 2 = 32
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 24
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/1YkvY
function name:  treeify
number of ops:  37
compiled vars:  !0 = $input, !1 = $result, !2 = $path, !3 = $entry, !4 = $depth
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        ASSIGN                                                   !1, <array>
    6     2        ASSIGN                                                   !2, <array>
    8     3      > FE_RESET_RW                                      $7      !0, ->32
          4    > > FE_FETCH_RW                                              $7, !3, ->32
    9     5    >   ASSIGN_DIM                                               !3, 'children'
          6        OP_DATA                                                  <array>
   11     7        FETCH_DIM_R                                      ~9      !3, 'depth'
          8        SUB                                              ~10     ~9, 1
          9        ASSIGN                                                   !4, ~10
   12    10        INIT_FCALL                                               'array_slice'
         11        SEND_VAR                                                 !2
         12        SEND_VAL                                                 0
         13        SEND_VAR                                                 !4
         14        DO_ICALL                                         $12     
         15        ASSIGN                                                   !2, $12
   13    16        FETCH_DIM_W                                      $14     !2
         17        ASSIGN_REF                                               $14, !3
   15    18        IS_IDENTICAL                                             !4, 0
         19      > JMPZ                                                     ~16, ->24
   16    20    >   FETCH_DIM_W                                      $17     !1
         21        ASSIGN_REF                                       $18     $17, !3
         22        QM_ASSIGN                                        ~19     $18
         23      > JMP                                                      ->30
   17    24    >   SUB                                              ~20     !4, 1
         25        FETCH_DIM_W                                      $21     !2, ~20
         26        FETCH_DIM_W                                      $22     $21, 'children'
         27        FETCH_DIM_W                                      $23     $22
         28        ASSIGN_REF                                       $24     $23, !3
         29        QM_ASSIGN                                        ~19     $24
         30    >   FREE                                                     ~19
    8    31      > JMP                                                      ->4
         32    >   FE_FREE                                                  $7
   20    33        VERIFY_RETURN_TYPE                                       !1
         34      > RETURN                                                   !1
   21    35*       VERIFY_RETURN_TYPE                                       
         36*     > RETURN                                                   null

End of function treeify

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.14 ms | 1022 KiB | 16 Q