3v4l.org

run code in 300+ PHP versions simultaneously
<?php ## the user "class" class Foo { public int $id; public string $foo; public ?string $group = null; public function __construct(int $id, string $foo) { $this->id = $id; $this->foo = $foo; } } ## users db result $users_array = [ new Foo(999, 'abc'), new Foo(4, 'abc'), new Foo(5, 'xyz'), new Foo(8, 'xxx') ]; ## get IDs and build index $index = []; $ids = array_reduce($users_array, function(?array $ids, Foo $object) use(&$index) { $ids[] = $object->id; $index[$object->id] = $object; return $ids; }); ### Groups, fetched from SQL $groups = [ [ 'id' => 999, 'group' => 'group1'], [ 'id' => 8, 'group' => 'group2'], [ 'id' => 4, 'group' => 'group3'], ]; ### stiching foreach ($groups as $row) { $index[$row['id']]->group = $row['group']; } ## now the original array of users have their "group" property populated from the groups query var_dump($users_array);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 31, Position 2 = 38
Branch analysis from position: 31
2 jumps found. (Code = 78) Position 1 = 32, Position 2 = 38
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
filename:       /in/KQ33q
function name:  (null)
number of ops:  43
compiled vars:  !0 = $users_array, !1 = $index, !2 = $ids, !3 = $groups, !4 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   NEW                                              $5      'Foo'
          1        SEND_VAL_EX                                              999
          2        SEND_VAL_EX                                              'abc'
          3        DO_FCALL                                      0          
          4        INIT_ARRAY                                       ~7      $5
   18     5        NEW                                              $8      'Foo'
          6        SEND_VAL_EX                                              4
          7        SEND_VAL_EX                                              'abc'
          8        DO_FCALL                                      0          
          9        ADD_ARRAY_ELEMENT                                ~7      $8
   19    10        NEW                                              $10     'Foo'
         11        SEND_VAL_EX                                              5
         12        SEND_VAL_EX                                              'xyz'
         13        DO_FCALL                                      0          
         14        ADD_ARRAY_ELEMENT                                ~7      $10
   20    15        NEW                                              $12     'Foo'
         16        SEND_VAL_EX                                              8
         17        SEND_VAL_EX                                              'xxx'
         18        DO_FCALL                                      0          
         19        ADD_ARRAY_ELEMENT                                ~7      $12
   16    20        ASSIGN                                                   !0, ~7
   24    21        ASSIGN                                                   !1, <array>
   25    22        INIT_FCALL                                               'array_reduce'
         23        SEND_VAR                                                 !0
         24        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FKQ33q%3A25%240'
         25        BIND_LEXICAL                                             ~16, !1
   30    26        SEND_VAL                                                 ~16
         27        DO_ICALL                                         $17     
   25    28        ASSIGN                                                   !2, $17
   33    29        ASSIGN                                                   !3, <array>
   41    30      > FE_RESET_R                                       $20     !3, ->38
         31    > > FE_FETCH_R                                               $20, !4, ->38
   42    32    >   FETCH_DIM_R                                      ~21     !4, 'id'
         33        FETCH_DIM_R                                      ~24     !4, 'group'
         34        FETCH_DIM_W                                      $22     !1, ~21
         35        ASSIGN_OBJ                                               $22, 'group'
         36        OP_DATA                                                  ~24
   41    37      > JMP                                                      ->31
         38    >   FE_FREE                                                  $20
   46    39        INIT_FCALL                                               'var_dump'
         40        SEND_VAR                                                 !0
         41        DO_ICALL                                                 
         42      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FKQ33q%3A25%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KQ33q
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $ids, !1 = $object, !2 = $index
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   26     3        FETCH_OBJ_R                                      ~4      !1, 'id'
          4        ASSIGN_DIM                                               !0
          5        OP_DATA                                                  ~4
   27     6        FETCH_OBJ_R                                      ~5      !1, 'id'
          7        ASSIGN_DIM                                               !2, ~5
          8        OP_DATA                                                  !1
   29     9      > RETURN                                                   !0
   30    10*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FKQ33q%3A25%240

Class Foo:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KQ33q
function name:  __construct
number of ops:  7
compiled vars:  !0 = $id, !1 = $foo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   10     2        ASSIGN_OBJ                                               'id'
          3        OP_DATA                                                  !0
   11     4        ASSIGN_OBJ                                               'foo'
          5        OP_DATA                                                  !1
   12     6      > RETURN                                                   null

End of function __construct

End of class Foo.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
127.05 ms | 1405 KiB | 17 Q