3v4l.org

run code in 300+ PHP versions simultaneously
<?php function chunk_iterator(Iterator $it, int $n) { $chunk = []; for($i = 0; $it->valid(); $i++){ $chunk[] = $it->current(); $it->next(); if(count($chunk) == $n){ yield $chunk; $chunk = []; } } if(count($chunk)){ yield $chunk; } } // For demonstration, create an iterator from array $arr = range(20, 40, 1); $it = new ArrayIterator($arr); // Iterate over the generator foreach(chunk_iterator($it, 6) as $c){ echo implode(',', $c)."\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 23
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 23
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/dMjCI
function name:  (null)
number of ops:  25
compiled vars:  !0 = $arr, !1 = $it, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   INIT_FCALL                                               'range'
          1        SEND_VAL                                                 20
          2        SEND_VAL                                                 40
          3        SEND_VAL                                                 1
          4        DO_ICALL                                         $3      
          5        ASSIGN                                                   !0, $3
   23     6        NEW                                              $5      'ArrayIterator'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !1, $5
   26    10        INIT_FCALL                                               'chunk_iterator'
         11        SEND_VAR                                                 !1
         12        SEND_VAL                                                 6
         13        DO_FCALL                                      0  $8      
         14      > FE_RESET_R                                       $9      $8, ->23
         15    > > FE_FETCH_R                                               $9, !2, ->23
   27    16    >   INIT_FCALL                                               'implode'
         17        SEND_VAL                                                 '%2C'
         18        SEND_VAR                                                 !2
         19        DO_ICALL                                         $10     
         20        CONCAT                                           ~11     $10, '%0A'
         21        ECHO                                                     ~11
   26    22      > JMP                                                      ->15
         23    >   FE_FREE                                                  $9
   28    24      > RETURN                                                   1

Function chunk_iterator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 6
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 24
Branch analysis from position: 23
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 24
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 6
Branch analysis from position: 21
Branch analysis from position: 6
Branch analysis from position: 17
filename:       /in/dMjCI
function name:  chunk_iterator
number of ops:  25
compiled vars:  !0 = $it, !1 = $n, !2 = $chunk, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        GENERATOR_CREATE                                         
    5     3        ASSIGN                                                   !2, <array>
    7     4        ASSIGN                                                   !3, 0
          5      > JMP                                                      ->18
    8     6    >   INIT_METHOD_CALL                                         !0, 'current'
          7        DO_FCALL                                      0  $7      
          8        ASSIGN_DIM                                               !2
          9        OP_DATA                                                  $7
    9    10        INIT_METHOD_CALL                                         !0, 'next'
         11        DO_FCALL                                      0          
   10    12        COUNT                                            ~9      !2
         13        IS_EQUAL                                                 !1, ~9
         14      > JMPZ                                                     ~10, ->17
   11    15    >   YIELD                                                    !2
   12    16        ASSIGN                                                   !2, <array>
    7    17    >   PRE_INC                                                  !3
         18    >   INIT_METHOD_CALL                                         !0, 'valid'
         19        DO_FCALL                                      0  $14     
         20      > JMPNZ                                                    $14, ->6
   16    21    >   COUNT                                            ~15     !2
         22      > JMPZ                                                     ~15, ->24
   17    23    >   YIELD                                                    !2
   19    24    > > GENERATOR_RETURN                                         

End of function chunk_iterator

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
167.26 ms | 1403 KiB | 18 Q