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, count($grouped) === 1 ? null : $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 = 37
Branch analysis from position: 16
2 jumps found. (Code = 126) Position 1 = 17, Position 2 = 37
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 27
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 36
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 36
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 36
Branch analysis from position: 34
Branch analysis from position: 36
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
Branch analysis from position: 10
filename:       /in/rFW55
function name:  (null)
number of ops:  42
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, ->37
         16    > > FE_FETCH_RW                                              $15, !5, ->37
   43    17    >   INIT_FCALL                                               'array_push'
         18        SEND_REF                                                 !4
         19        INIT_FCALL                                               'array_splice'
         20        SEND_REF                                                 !5
         21        SEND_VAL                                                 0
         22        COUNT                                            ~16     !2
         23        IS_IDENTICAL                                             ~16, 1
         24      > JMPZ                                                     ~17, ->27
         25    >   QM_ASSIGN                                        ~18     null
         26      > JMP                                                      ->28
         27    >   QM_ASSIGN                                        ~18     !1
         28    >   SEND_VAL                                                 ~18
         29        DO_ICALL                                         $19     
         30        SEND_UNPACK                                              $19
         31        CHECK_UNDEF_ARGS                                         
         32        DO_ICALL                                                 
   44    33      > JMPZ                                                     !5, ->36
   45    34    >   ASSIGN_DIM                                               !2
         35        OP_DATA                                                  !5
   42    36    > > JMP                                                      ->16
         37    >   FE_FREE                                                  $15
   48    38        INIT_FCALL                                               'var_export'
         39        SEND_VAR                                                 !4
         40        DO_ICALL                                                 
         41      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.77 ms | 1006 KiB | 17 Q