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"; }

preferences:
31.34 ms | 404 KiB | 5 Q