3v4l.org

run code in 500+ PHP versions simultaneously
<?php $array = [ /* | */['id' => 1, 'depth' => 1], /* |----| */ ['id' => 19, 'depth' => 2], /* |----|----| */ ['id' => 20, 'depth' => 3], /* |----| */ ['id' => 73, 'depth' => 2], /* | */ ['id' => 3, 'depth' => 1], /* |----| */ ['id' => 25, 'depth' => 2], ]; $root = new stdClass(); $bread_crumbs = [$root]; foreach ($array as $item) { // make it object to simplify access to its parts $object_item = (object) $item; $expected_depth = count($bread_crumbs); if ($object_item->depth < 1) { throw new Exception('Depth cannot be lower than 1'); } elseif ($object_item->depth == $expected_depth) { // doing nothing } elseif ($object_item->depth < $expected_depth) { // remove 'unwanted' items to make expected_depth actual array_splice($bread_crumbs, -($expected_depth - $object_item->depth)); } else { throw new Exception('Error cannot jump too high'); } end($bread_crumbs)->children[] = $object_item; // and then just add to the end of bread_crumbs current object_item $bread_crumbs[] = $object_item; } var_dump($root);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 49
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 49
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 20
Branch analysis from position: 15
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 24
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 35
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
Branch analysis from position: 35
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 49
filename:       /in/YQgR1
function name:  (null)
number of ops:  54
compiled vars:  !0 = $array, !1 = $root, !2 = $bread_crumbs, !3 = $item, !4 = $object_item, !5 = $expected_depth
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   12     1        NEW                                                  $7      'stdClass'
          2        DO_FCALL                                          0          
          3        ASSIGN                                                       !1, $7
   13     4        INIT_ARRAY                                           ~10     !1
          5        ASSIGN                                                       !2, ~10
   14     6      > FE_RESET_R                                           $12     !0, ->49
          7    > > FE_FETCH_R                                                   $12, !3, ->49
   16     8    >   CAST                                              8  ~13     !3
          9        ASSIGN                                                       !4, ~13
   17    10        COUNT                                                ~15     !2
         11        ASSIGN                                                       !5, ~15
   18    12        FETCH_OBJ_R                                          ~17     !4, 'depth'
         13        IS_SMALLER                                                   ~17, 1
         14      > JMPZ                                                         ~18, ->20
   19    15    >   NEW                                                  $19     'Exception'
         16        SEND_VAL_EX                                                  'Depth+cannot+be+lower+than+1'
         17        DO_FCALL                                          0          
         18      > THROW                                             0          $19
   18    19*       JMP                                                          ->39
   20    20    >   FETCH_OBJ_R                                          ~21     !4, 'depth'
         21        IS_EQUAL                                                     !5, ~21
         22      > JMPZ                                                         ~22, ->24
         23    > > JMP                                                          ->39
   22    24    >   FETCH_OBJ_R                                          ~23     !4, 'depth'
         25        IS_SMALLER                                                   ~23, !5
         26      > JMPZ                                                         ~24, ->35
   24    27    >   INIT_FCALL                                                   'array_splice'
         28        SEND_REF                                                     !2
         29        FETCH_OBJ_R                                          ~25     !4, 'depth'
         30        SUB                                                  ~26     !5, ~25
         31        MUL                                                  ~27     ~26, -1
         32        SEND_VAL                                                     ~27
         33        DO_ICALL                                                     
   22    34      > JMP                                                          ->39
   26    35    >   NEW                                                  $29     'Exception'
         36        SEND_VAL_EX                                                  'Error+cannot+jump+too+high'
         37        DO_FCALL                                          0          
         38      > THROW                                             0          $29
   29    39    >   INIT_FCALL                                                   'end'
         40        SEND_REF                                                     !2
         41        DO_ICALL                                             $31     
         42        SEPARATE                                             $31     $31
         43        FETCH_OBJ_W                                          $32     $31, 'children'
         44        ASSIGN_DIM                                                   $32
         45        OP_DATA                                                      !4
   32    46        ASSIGN_DIM                                                   !2
         47        OP_DATA                                                      !4
   14    48      > JMP                                                          ->7
         49    >   FE_FREE                                                      $12
   34    50        INIT_FCALL                                                   'var_dump'
         51        SEND_VAR                                                     !1
         52        DO_ICALL                                                     
         53      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
225.3 ms | 2778 KiB | 16 Q