3v4l.org

run code in 300+ PHP versions simultaneously
<?php $originalArray = array( array('id' => 1, 'sub-id' => 0), array('id' => 2, 'sub-id' => 0), array('id' => 3, 'sub-id' => 1), array('id' => 4, 'sub-id' => 3), array('id' => 5, 'sub-id' => 4), array('id' => 6, 'sub-id' => 0), array('id' => 7, 'sub-id' => 0), array('id' => 8, 'sub-id' => 6), array('id' => 9, 'sub-id' => 8), array('id' => 10, 'sub-id' => 8) ); foreach ($originalArray as $val) { $array[$val["id"]] = $val; } // we first assign the arrays in a non-destructive way, so that we can easily find the // appropriate key in the array foreach ($array as $key => $val) { if ($val["sub-id"] !== 0) { $array[$val["sub-id"]]["sub"][] = & $array[$key]; } } // remove the ones from the first dimension which are somewhere deeper foreach ($array as $key => $val) { if ($val["sub-id"] !== 0) { unset($array[$key]); } } print_r($array);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 7
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 22
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 22
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 21
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 21
Branch analysis from position: 22
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 31
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 31
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 30
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 30
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
Branch analysis from position: 22
Branch analysis from position: 7
filename:       /in/51UgR
function name:  (null)
number of ops:  36
compiled vars:  !0 = $originalArray, !1 = $val, !2 = $array, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   16     1      > FE_RESET_R                                       $5      !0, ->7
          2    > > FE_FETCH_R                                               $5, !1, ->7
   17     3    >   FETCH_DIM_R                                      ~6      !1, 'id'
          4        ASSIGN_DIM                                               !2, ~6
          5        OP_DATA                                                  !1
   16     6      > JMP                                                      ->2
          7    >   FE_FREE                                                  $5
   22     8      > FE_RESET_R                                       $8      !2, ->22
          9    > > FE_FETCH_R                                       ~9      $8, !1, ->22
         10    >   ASSIGN                                                   !3, ~9
   23    11        FETCH_DIM_R                                      ~11     !1, 'sub-id'
         12        IS_NOT_IDENTICAL                                         ~11, 0
         13      > JMPZ                                                     ~12, ->21
   24    14    >   FETCH_DIM_R                                      ~13     !1, 'sub-id'
         15        FETCH_DIM_W                                      $17     !2, !3
         16        MAKE_REF                                         $18     $17
         17        FETCH_DIM_W                                      $14     !2, ~13
         18        FETCH_DIM_W                                      $15     $14, 'sub'
         19        FETCH_DIM_W                                      $16     $15
         20        ASSIGN_REF                                               $16, $18
   22    21    > > JMP                                                      ->9
         22    >   FE_FREE                                                  $8
   29    23      > FE_RESET_R                                       $20     !2, ->31
         24    > > FE_FETCH_R                                       ~21     $20, !1, ->31
         25    >   ASSIGN                                                   !3, ~21
   30    26        FETCH_DIM_R                                      ~23     !1, 'sub-id'
         27        IS_NOT_IDENTICAL                                         ~23, 0
         28      > JMPZ                                                     ~24, ->30
   31    29    >   UNSET_DIM                                                !2, !3
   29    30    > > JMP                                                      ->24
         31    >   FE_FREE                                                  $20
   35    32        INIT_FCALL                                               'print_r'
         33        SEND_VAR                                                 !2
         34        DO_ICALL                                                 
         35      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.73 ms | 1392 KiB | 15 Q