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; array_multisort( array_map( function ($row) use ($maxConsecutive) { static $encountered; $encountered[$row['type']] ??= 0; return intdiv($encountered[$row['type']]++, $maxConsecutive); }, $items ), array_column($items, 'type'), $items ); var_export($items);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
array ( 0 => array ( 'type' => 1, 'text' => 'A', ), 1 => array ( 'type' => 1, 'text' => 'F', ), 2 => array ( 'type' => 1, 'text' => 'I', ), 3 => array ( 'type' => 2, 'text' => 'B', ), 4 => array ( 'type' => 2, 'text' => 'C', ), 5 => array ( 'type' => 2, 'text' => 'E', ), 6 => array ( 'type' => 3, 'text' => 'D', ), 7 => array ( 'type' => 3, 'text' => 'N', ), 8 => array ( 'type' => 3, 'text' => 'O', ), 9 => array ( 'type' => 4, 'text' => 'G', ), 10 => array ( 'type' => 4, 'text' => 'J', ), 11 => array ( 'type' => 4, 'text' => 'L', ), 12 => array ( 'type' => 1, 'text' => 'P', ), 13 => array ( 'type' => 1, 'text' => 'Q', ), 14 => array ( 'type' => 2, 'text' => 'H', ), 15 => array ( 'type' => 2, 'text' => 'K', ), 16 => array ( 'type' => 2, 'text' => 'M', ), 17 => array ( 'type' => 3, 'text' => 'S', ), 18 => array ( 'type' => 3, 'text' => 'U', ), 19 => array ( 'type' => 3, 'text' => 'W', ), 20 => array ( 'type' => 4, 'text' => 'Y', ), 21 => array ( 'type' => 2, 'text' => 'R', ), 22 => array ( 'type' => 2, 'text' => 'T', ), 23 => array ( 'type' => 2, 'text' => 'V', ), 24 => array ( 'type' => 2, 'text' => 'X', ), 25 => array ( 'type' => 2, 'text' => 'Z', ), )

preferences:
100.54 ms | 406 KiB | 91 Q