<?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);
preferences:
23.28 ms | 408 KiB | 5 Q