3v4l.org

run code in 300+ PHP versions simultaneously
<?php function chained(iterable ...$input): Generator { foreach ($input as $iterable) { yield from $iterable; } } echo "The \"chained\" elements using a generator: "; foreach(chained( [1,2,3], [4,5], ) as $item){ echo "$item "; } echo "\n\n"; echo "However, when it comes to converting the generator to array, there is a caveat:\n"; $numbers = chained( [1,2,3], [4,5], ); print_r( iterator_to_array($numbers) ); echo "\n"; echo "The issue is caused by overlapping keys. See how they appear in the sequence:\n"; foreach(chained( [1,2,3], [4,5], ) as $key => $item){ echo "[$key] => $item\n"; } echo "\n\n"; echo "The issue is not present when the keys do not overlap:\n"; $numbers = chained( [1,2,3], [10 => 4,5], ); print_r( iterator_to_array($numbers) );
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 41
Branch analysis from position: 32
2 jumps found. (Code = 78) Position 1 = 33, Position 2 = 41
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
Branch analysis from position: 11
filename:       /in/DAIQV
function name:  (null)
number of ops:  56
compiled vars:  !0 = $item, !1 = $numbers, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   ECHO                                                     'The+%22chained%22+elements+using+a+generator%3A+'
   11     1        INIT_FCALL                                               'chained'
   12     2        SEND_VAL                                                 <array>
   13     3        SEND_VAL                                                 <array>
   11     4        DO_FCALL                                      0  $3      
   13     5      > FE_RESET_R                                       $4      $3, ->11
          6    > > FE_FETCH_R                                               $4, !0, ->11
   15     7    >   NOP                                                      
          8        FAST_CONCAT                                      ~5      !0, '+'
          9        ECHO                                                     ~5
   11    10      > JMP                                                      ->6
         11    >   FE_FREE                                                  $4
   17    12        ECHO                                                     '%0A%0A'
   19    13        ECHO                                                     'However%2C+when+it+comes+to+converting+the+generator+to+array%2C+there+is+a+caveat%3A%0A'
   21    14        INIT_FCALL                                               'chained'
   22    15        SEND_VAL                                                 <array>
   23    16        SEND_VAL                                                 <array>
   21    17        DO_FCALL                                      0  $6      
         18        ASSIGN                                                   !1, $6
   25    19        INIT_FCALL                                               'print_r'
   26    20        INIT_FCALL                                               'iterator_to_array'
         21        SEND_VAR                                                 !1
         22        DO_ICALL                                         $8      
         23        SEND_VAR                                                 $8
   25    24        DO_ICALL                                                 
   29    25        ECHO                                                     '%0A'
   30    26        ECHO                                                     'The+issue+is+caused+by+overlapping+keys.+See+how+they+appear+in+the+sequence%3A%0A'
   32    27        INIT_FCALL                                               'chained'
   33    28        SEND_VAL                                                 <array>
   34    29        SEND_VAL                                                 <array>
   32    30        DO_FCALL                                      0  $10     
   34    31      > FE_RESET_R                                       $11     $10, ->41
         32    > > FE_FETCH_R                                       ~12     $11, !0, ->41
   35    33    >   ASSIGN                                                   !2, ~12
   36    34        ROPE_INIT                                     5  ~15     '%5B'
         35        ROPE_ADD                                      1  ~15     ~15, !2
         36        ROPE_ADD                                      2  ~15     ~15, '%5D+%3D%3E+'
         37        ROPE_ADD                                      3  ~15     ~15, !0
         38        ROPE_END                                      4  ~14     ~15, '%0A'
         39        ECHO                                                     ~14
   32    40      > JMP                                                      ->32
         41    >   FE_FREE                                                  $11
   38    42        ECHO                                                     '%0A%0A'
   40    43        ECHO                                                     'The+issue+is+not+present+when+the+keys+do+not+overlap%3A%0A'
   42    44        INIT_FCALL                                               'chained'
   43    45        SEND_VAL                                                 <array>
   44    46        SEND_VAL                                                 <array>
   42    47        DO_FCALL                                      0  $18     
         48        ASSIGN                                                   !1, $18
   46    49        INIT_FCALL                                               'print_r'
   47    50        INIT_FCALL                                               'iterator_to_array'
         51        SEND_VAR                                                 !1
         52        DO_ICALL                                         $20     
         53        SEND_VAR                                                 $20
   46    54        DO_ICALL                                                 
   48    55      > RETURN                                                   1

Function chained:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 7
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 7
filename:       /in/DAIQV
function name:  chained
number of ops:  9
compiled vars:  !0 = $input, !1 = $iterable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV_VARIADIC                                    !0      
          1        GENERATOR_CREATE                                         
    5     2      > FE_RESET_R                                       $2      !0, ->7
          3    > > FE_FETCH_R                                               $2, !1, ->7
    6     4    >   YIELD_FROM                                       ~3      !1
          5        FREE                                                     ~3
    5     6      > JMP                                                      ->3
          7    >   FE_FREE                                                  $2
    8     8      > GENERATOR_RETURN                                         

End of function chained

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.46 ms | 1445 KiB | 19 Q