3v4l.org

run code in 300+ PHP versions simultaneously
<?php const DATA = [ [1070, 1023, 'this is the first record'], [1070, 1028, 'this is the second record'], [1098, 1023, 'this is the thrid record'], [1098, 1028, 'this is the fourth record'], [2021, 1023, 'this is the fifth record'], [2021, 1028, 'this is the sixth record'], ]; function group_records(array $data, int $col) { $last = null; $current = []; foreach (DATA as $row) { if ($row[$col] !== $last && $last !== null) { yield $last => $current; $last = null; $current = []; } $last = $row[$col]; $current[] = $row; } if ($last !== null) { yield $last => $current; } } foreach (group_records(DATA, 0) as $key => $rows) { echo "$key:\n"; foreach ($rows as $row) echo " " . implode('|', $row) . "\n"; }
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
1070: 1070|1023|this is the first record 1070|1028|this is the second record 1098: 1098|1023|this is the thrid record 1098|1028|this is the fourth record 2021: 2021|1023|this is the fifth record 2021|1028|this is the sixth record

preferences:
132.16 ms | 408 KiB | 5 Q