<?php
$dataset = [
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 6, 7, 8, 9, 12, 13, 14, 15],
[94, 95],
[0, 1, 94, 95],
];
function arrange($array)
{
$groups = [];
$group = null;
foreach ($array as $index => $current) {
if ($group === null) {
$group = [
'start' => $current,
'end' => $current
];
continue;
}
$group['end'] = $current;
$next = $index + 1;
if (array_key_exists($next, $array)) {
if ($array[$next] - $current !== 1) {
array_push($groups, $group);
$group = null;
}
}
}
if ($group !== null) {
array_push($groups, $group);
}
return $groups;
}
foreach ($dataset as $array) {
$arrange = arrange($array);
print_r($arrange);
}