3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ [ 'group_id' => 5, 'group_title' => 'Test group', 'group_parent_id' => 2 ], [ 'group_id' => 6, 'group_title' => 'Second-level test group', 'group_parent_id' => 5 ], [ 'group_id' => 7, 'group_title' => 'Third-level test group', 'group_parent_id' => 6 ], [ 'group_id' => 8, 'group_title' => 'Other test group', 'group_parent_id' => 1 ], [ 'group_id' => 9, 'group_title' => 'Second-level other test group', 'group_parent_id' => 8 ] ]; $output = array(); $group_ids = array(); foreach ($arr as $el) { $parent_id = $el['group_parent_id']; if (!isset($group_ids[$parent_id])) { // must be a top-level element $output[] = $el; // save a pointer $group_ids[$el['group_id']] = &$output[array_key_last($output)]; } else { // already exists, $group_ids[$parent_id] points to the entry $parent = &$group_ids[$parent_id]; if (!isset($parent['subgroups'])) { // no subgroups array, create one $parent['subgroups'] = array(); } // add this entry to the subgroups $parent['subgroups'][] = $el; // save a pointer $group_ids[$el['group_id']] = &$parent['subgroups'][array_key_last($parent['subgroups'])]; } } print_r($output);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 42
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 42
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 21
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 28
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 28
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
filename:       /in/LkgLv
function name:  (null)
number of ops:  47
compiled vars:  !0 = $arr, !1 = $output, !2 = $group_ids, !3 = $el, !4 = $parent_id, !5 = $parent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   31     1        ASSIGN                                                   !1, <array>
   32     2        ASSIGN                                                   !2, <array>
   33     3      > FE_RESET_R                                       $9      !0, ->42
          4    > > FE_FETCH_R                                               $9, !3, ->42
   34     5    >   FETCH_DIM_R                                      ~10     !3, 'group_parent_id'
          6        ASSIGN                                                   !4, ~10
   35     7        ISSET_ISEMPTY_DIM_OBJ                         0  ~12     !2, !4
          8        BOOL_NOT                                         ~13     ~12
          9      > JMPZ                                                     ~13, ->21
   37    10    >   ASSIGN_DIM                                               !1
         11        OP_DATA                                                  !3
   39    12        FETCH_DIM_R                                      ~15     !3, 'group_id'
         13        INIT_FCALL                                               'array_key_last'
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                         $17     
         16        FETCH_DIM_W                                      $18     !1, $17
         17        MAKE_REF                                         $19     $18
         18        FETCH_DIM_W                                      $16     !2, ~15
         19        ASSIGN_REF                                               $16, $19
   35    20      > JMP                                                      ->41
   43    21    >   FETCH_DIM_W                                      $21     !2, !4
         22        ASSIGN_REF                                               !5, $21
   44    23        ISSET_ISEMPTY_DIM_OBJ                         0  ~23     !5, 'subgroups'
         24        BOOL_NOT                                         ~24     ~23
         25      > JMPZ                                                     ~24, ->28
   46    26    >   ASSIGN_DIM                                               !5, 'subgroups'
         27        OP_DATA                                                  <array>
   49    28    >   FETCH_DIM_W                                      $26     !5, 'subgroups'
         29        ASSIGN_DIM                                               $26
         30        OP_DATA                                                  !3
   51    31        FETCH_DIM_R                                      ~28     !3, 'group_id'
         32        INIT_FCALL                                               'array_key_last'
         33        FETCH_DIM_R                                      ~31     !5, 'subgroups'
         34        SEND_VAL                                                 ~31
         35        DO_ICALL                                         $32     
         36        FETCH_DIM_W                                      $30     !5, 'subgroups'
         37        FETCH_DIM_W                                      $33     $30, $32
         38        MAKE_REF                                         $34     $33
         39        FETCH_DIM_W                                      $29     !2, ~28
         40        ASSIGN_REF                                               $29, $34
   33    41    > > JMP                                                      ->4
         42    >   FE_FREE                                                  $9
   54    43        INIT_FCALL                                               'print_r'
         44        SEND_VAR                                                 !1
         45        DO_ICALL                                                 
         46      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
136.2 ms | 1004 KiB | 15 Q