3v4l.org

run code in 300+ PHP versions simultaneously
<?php // use ORDER BY belongs_to DESC, id ASC ... or usort() to prepare result set $resultset = [ ['id' => '6a', 'belongs_to' => '5a'], ['id' => '5a', 'belongs_to' => '3a'], ['id' => '8a', 'belongs_to' => '3a'], ['id' => '3a', 'belongs_to' => '1a'], ['id' => '1a', 'belongs_to' => null], ['id' => '2a', 'belongs_to' => null], ['id' => '4a', 'belongs_to' => null], ['id' => '7a', 'belongs_to' => null] ]; foreach ($resultset as $index1 => &$row1) { // make input array modifiable by reference (not working with a copy) if ($row1['belongs_to']) { // original belongs_to value is not null (not a top-level parent) foreach ($resultset as $index2 => $row2) { // search for targeted parent if ($row2['id'] == $row1['belongs_to']) { // parent found $resultset[$index2]['children'][] = [$row1['id'] => $row1['children'] ?? []]; // store original row as child unset($resultset[$index1]); // remove original row (no reason to iterate it again in outer loop) break; // halt inner loop (no reason to iterate further) } } } else { // original belongs_to value is null (top-level parent) $output[$row1['id']] = $row1['children'] ?? []; // store children to top } } var_export($output);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 2, Position 2 = 34
Branch analysis from position: 2
2 jumps found. (Code = 126) Position 1 = 3, Position 2 = 34
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 27
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 25
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 25
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 24
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 25
Branch analysis from position: 25
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
filename:       /in/97S5I
function name:  (null)
number of ops:  39
compiled vars:  !0 = $resultset, !1 = $row1, !2 = $index1, !3 = $row2, !4 = $index2, !5 = $output
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   14     1      > FE_RESET_RW                                      $7      !0, ->34
          2    > > FE_FETCH_RW                                      ~8      $7, !1, ->34
          3    >   ASSIGN                                                   !2, ~8
   15     4        FETCH_DIM_R                                      ~10     !1, 'belongs_to'
          5      > JMPZ                                                     ~10, ->27
   16     6    > > FE_RESET_R                                       $11     !0, ->25
          7    > > FE_FETCH_R                                       ~12     $11, !3, ->25
          8    >   ASSIGN                                                   !4, ~12
   17     9        FETCH_DIM_R                                      ~14     !3, 'id'
         10        FETCH_DIM_R                                      ~15     !1, 'belongs_to'
         11        IS_EQUAL                                                 ~14, ~15
         12      > JMPZ                                                     ~16, ->24
   18    13    >   FETCH_DIM_R                                      ~20     !1, 'id'
         14        FETCH_DIM_IS                                     ~21     !1, 'children'
         15        COALESCE                                         ~22     ~21
         16        QM_ASSIGN                                        ~22     <array>
         17        INIT_ARRAY                                       ~23     ~22, ~20
         18        FETCH_DIM_W                                      $17     !0, !4
         19        FETCH_DIM_W                                      $18     $17, 'children'
         20        ASSIGN_DIM                                               $18
         21        OP_DATA                                                  ~23
   19    22        UNSET_DIM                                                !0, !2
   20    23      > JMP                                                      ->25
   16    24    > > JMP                                                      ->7
         25    >   FE_FREE                                                  $11
         26      > JMP                                                      ->33
   24    27    >   FETCH_DIM_R                                      ~24     !1, 'id'
         28        FETCH_DIM_IS                                     ~26     !1, 'children'
         29        COALESCE                                         ~27     ~26
         30        QM_ASSIGN                                        ~27     <array>
         31        ASSIGN_DIM                                               !5, ~24
         32        OP_DATA                                                  ~27
   14    33    > > JMP                                                      ->2
         34    >   FE_FREE                                                  $7
   27    35        INIT_FCALL                                               'var_export'
         36        SEND_VAR                                                 !5
         37        DO_ICALL                                                 
         38      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
165.72 ms | 1400 KiB | 15 Q