3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); $items = [ (object)['id' => 1, 'parentId' => 0, 'name' => "foo"], (object)['id' => 2, 'parentId' => 1, 'name' => "bar"], (object)['id' => 3, 'parentId' => 1, 'name' => "qux"], (object)['id' => 4, 'parentId' => 3, 'name' => "zip"], (object)['id' => 5, 'parentId' => 4, 'name' => "baz"] ]; $itemsById = []; foreach ($items as $item) { $itemsById[$item->id] = $item; } // --- foreach ($itemsById as $id => $item) { $itemsById[$item->parentId]->children[] = $item; } var_dump($itemsById);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 18
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 18
2 jumps found. (Code = 77) Position 1 = 20, Position 2 = 28
Branch analysis from position: 20
2 jumps found. (Code = 78) Position 1 = 21, Position 2 = 28
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
Branch analysis from position: 18
filename:       /in/OBh3k
function name:  (null)
number of ops:  33
compiled vars:  !0 = $items, !1 = $itemsById, !2 = $item, !3 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   CAST                                          8  ~4      <array>
          1        INIT_ARRAY                                       ~5      ~4
    6     2        CAST                                          8  ~6      <array>
          3        ADD_ARRAY_ELEMENT                                ~5      ~6
    7     4        CAST                                          8  ~7      <array>
          5        ADD_ARRAY_ELEMENT                                ~5      ~7
    8     6        CAST                                          8  ~8      <array>
          7        ADD_ARRAY_ELEMENT                                ~5      ~8
    9     8        CAST                                          8  ~9      <array>
          9        ADD_ARRAY_ELEMENT                                ~5      ~9
    4    10        ASSIGN                                                   !0, ~5
   12    11        ASSIGN                                                   !1, <array>
   13    12      > FE_RESET_R                                       $12     !0, ->18
         13    > > FE_FETCH_R                                               $12, !2, ->18
   14    14    >   FETCH_OBJ_R                                      ~13     !2, 'id'
         15        ASSIGN_DIM                                               !1, ~13
         16        OP_DATA                                                  !2
   13    17      > JMP                                                      ->13
         18    >   FE_FREE                                                  $12
   19    19      > FE_RESET_R                                       $15     !1, ->28
         20    > > FE_FETCH_R                                       ~16     $15, !2, ->28
         21    >   ASSIGN                                                   !3, ~16
   20    22        FETCH_OBJ_R                                      ~18     !2, 'parentId'
         23        FETCH_DIM_W                                      $19     !1, ~18
         24        FETCH_OBJ_W                                      $20     $19, 'children'
         25        ASSIGN_DIM                                               $20
         26        OP_DATA                                                  !2
   19    27      > JMP                                                      ->20
         28    >   FE_FREE                                                  $15
   23    29        INIT_FCALL                                               'var_dump'
         30        SEND_VAR                                                 !1
         31        DO_ICALL                                                 
         32      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.6 ms | 1396 KiB | 15 Q