3v4l.org

run code in 300+ PHP versions simultaneously
<?php $items = [ ['type' => 1, 'text' => 'A'], ['type' => 2, 'text' => 'B'], ['type' => 2, 'text' => 'C'], ['type' => 3, 'text' => 'D'], ['type' => 2, 'text' => 'E'], ['type' => 1, 'text' => 'F'], ['type' => 4, 'text' => 'G'], ['type' => 2, 'text' => 'H'], ['type' => 1, 'text' => 'I'], ['type' => 4, 'text' => 'J'], ['type' => 2, 'text' => 'K'], ['type' => 4, 'text' => 'L'], ['type' => 2, 'text' => 'M'], ['type' => 3, 'text' => 'N'], ['type' => 3, 'text' => 'O'], ['type' => 1, 'text' => 'P'], ['type' => 1, 'text' => 'Q'], ['type' => 2, 'text' => 'R'], ['type' => 3, 'text' => 'S'], ['type' => 2, 'text' => 'T'], ['type' => 3, 'text' => 'U'], ['type' => 2, 'text' => 'V'], ['type' => 3, 'text' => 'W'], ['type' => 2, 'text' => 'X'], ['type' => 4, 'text' => 'Y'], ['type' => 2, 'text' => 'Z'], ]; $maxConsecutive = 3; $grouped = []; foreach ($items as $row) { $grouped[$row['type']][] = $row; } ksort($grouped); $result = []; foreach ($grouped as &$group) { array_push($result, ...array_splice($group, 0, $maxConsecutive)); if ($group) { $grouped[] = $group; } } var_export($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 10
2 jumps found. (Code = 125) Position 1 = 16, Position 2 = 31
Branch analysis from position: 16
2 jumps found. (Code = 126) Position 1 = 17, Position 2 = 31
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 30
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
Branch analysis from position: 10
filename:       /in/Epa7C
function name:  (null)
number of ops:  36
compiled vars:  !0 = $items, !1 = $maxConsecutive, !2 = $grouped, !3 = $row, !4 = $result, !5 = $group
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   32     1        ASSIGN                                                   !1, 3
   34     2        ASSIGN                                                   !2, <array>
   35     3      > FE_RESET_R                                       $9      !0, ->10
          4    > > FE_FETCH_R                                               $9, !3, ->10
   36     5    >   FETCH_DIM_R                                      ~10     !3, 'type'
          6        FETCH_DIM_W                                      $11     !2, ~10
          7        ASSIGN_DIM                                               $11
          8        OP_DATA                                                  !3
   35     9      > JMP                                                      ->4
         10    >   FE_FREE                                                  $9
   39    11        INIT_FCALL                                               'ksort'
         12        SEND_REF                                                 !2
         13        DO_ICALL                                                 
   41    14        ASSIGN                                                   !4, <array>
   42    15      > FE_RESET_RW                                      $15     !2, ->31
         16    > > FE_FETCH_RW                                              $15, !5, ->31
   43    17    >   INIT_FCALL                                               'array_push'
         18        SEND_REF                                                 !4
         19        INIT_FCALL                                               'array_splice'
         20        SEND_REF                                                 !5
         21        SEND_VAL                                                 0
         22        SEND_VAR                                                 !1
         23        DO_ICALL                                         $16     
         24        SEND_UNPACK                                              $16
         25        CHECK_UNDEF_ARGS                                         
         26        DO_ICALL                                                 
   44    27      > JMPZ                                                     !5, ->30
   45    28    >   ASSIGN_DIM                                               !2
         29        OP_DATA                                                  !5
   42    30    > > JMP                                                      ->16
         31    >   FE_FREE                                                  $15
   48    32        INIT_FCALL                                               'var_export'
         33        SEND_VAR                                                 !4
         34        DO_ICALL                                                 
         35      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.28 ms | 1008 KiB | 17 Q