3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = array( array("id" => "Cosmetics", "parent" => "Beauty"), array("id" => "Makeup", "parent" => "Cosmetics"), array("id" => "Beauty", "parent" => null) ); $options = array(); $refs = array(); foreach($input as &$category) { $refs[$category["id"]] = $option = array( "id" => $category["id"], "options" => array() ); print_r($refs); if($category["parent"]){ if(!array_key_exists($category["parent"], $refs)) { $parent = array( "id" => $category["parent"], "options" => array( &$refs[$category["id"]] ) ); $refs[$parent["id"]] = &$refs[$parent["id"]]; } else { $refs[$category["parent"]]["options"][] = &$option; // print_r($refs[$category["parent"]]); } } else { $options[] = $refs[$category["id"]]; } } print_r($options);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 4, Position 2 = 46
Branch analysis from position: 4
2 jumps found. (Code = 126) Position 1 = 5, Position 2 = 46
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 41
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 35
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 46
filename:       /in/9UlC0
function name:  (null)
number of ops:  51
compiled vars:  !0 = $input, !1 = $options, !2 = $refs, !3 = $category, !4 = $option, !5 = $parent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
    8     1        ASSIGN                                                   !1, <array>
    9     2        ASSIGN                                                   !2, <array>
   11     3      > FE_RESET_RW                                      $9      !0, ->46
          4    > > FE_FETCH_RW                                              $9, !3, ->46
   12     5    >   FETCH_DIM_R                                      ~10     !3, 'id'
   13     6        FETCH_DIM_R                                      ~12     !3, 'id'
          7        INIT_ARRAY                                       ~13     ~12, 'id'
          8        ADD_ARRAY_ELEMENT                                ~13     <array>, 'options'
   12     9        ASSIGN                                           ~14     !4, ~13
         10        ASSIGN_DIM                                               !2, ~10
         11        OP_DATA                                                  ~14
   16    12        INIT_FCALL                                               'print_r'
         13        SEND_VAR                                                 !2
         14        DO_ICALL                                                 
   17    15        FETCH_DIM_R                                      ~16     !3, 'parent'
         16      > JMPZ                                                     ~16, ->41
   18    17    >   FETCH_DIM_R                                      ~17     !3, 'parent'
         18        ARRAY_KEY_EXISTS                                 ~18     ~17, !2
         19        BOOL_NOT                                         ~19     ~18
         20      > JMPZ                                                     ~19, ->35
   20    21    >   FETCH_DIM_R                                      ~20     !3, 'parent'
         22        INIT_ARRAY                                       ~21     ~20, 'id'
   22    23        FETCH_DIM_R                                      ~22     !3, 'id'
         24        FETCH_DIM_W                                      $23     !2, ~22
         25        INIT_ARRAY                                       ~24     $23
         26        ADD_ARRAY_ELEMENT                                ~21     ~24, 'options'
   19    27        ASSIGN                                                   !5, ~21
   25    28        FETCH_DIM_R                                      ~26     !5, 'id'
         29        FETCH_DIM_R                                      ~28     !5, 'id'
         30        FETCH_DIM_W                                      $29     !2, ~28
         31        MAKE_REF                                         $30     $29
         32        FETCH_DIM_W                                      $27     !2, ~26
         33        ASSIGN_REF                                               $27, $30
         34      > JMP                                                      ->40
   27    35    >   FETCH_DIM_R                                      ~32     !3, 'parent'
         36        FETCH_DIM_W                                      $33     !2, ~32
         37        FETCH_DIM_W                                      $34     $33, 'options'
         38        FETCH_DIM_W                                      $35     $34
         39        ASSIGN_REF                                               $35, !4
         40    > > JMP                                                      ->45
   31    41    >   FETCH_DIM_R                                      ~38     !3, 'id'
         42        FETCH_DIM_R                                      ~39     !2, ~38
         43        ASSIGN_DIM                                               !1
         44        OP_DATA                                                  ~39
   11    45    > > JMP                                                      ->4
         46    >   FE_FREE                                                  $9
   36    47        INIT_FCALL                                               'print_r'
         48        SEND_VAR                                                 !1
         49        DO_ICALL                                                 
         50      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
167.23 ms | 1406 KiB | 15 Q