<?php
$inputs = [
[
['Пн', '08:00 - 20:00'],
['Вт', '08:00 - 20:00'],
['Ср', '08:00 - 20:00'],
['Чт', '08:00 - 20:00'],
['Пт', '08:00 - 20:00'],
['Сб', '08:00 - 18:00'],
['Вс', '10:00 - 15:00'],
],
[
['Пн', '08:00 - 19:00'],
['Вт', '08:00 - 19:00'],
['Ср', '08:00 - 21:00'],
['Чт', '08:00 - 20:00'],
['Пт', '08:00 - 20:00'],
['Сб', '08:00 - 18:00'],
['Вс', '10:00 - 15:00'],
],
];
function is_consecutive(array $numbers)
{
$count = count($numbers);
if ($count < 2) {
return false;
}
$first = $numbers[0];
$range = range($first, $first + $count - 1);
return $numbers === $range;
}
function format_day_times(array $input)
{
$timeIndexes = [];
foreach ($input as $index => list($day, $time)) {
$timeIndexes[$time][] = [$index, $day];
}
$result = [];
foreach ($timeIndexes as $time => $data) {
if (is_consecutive(array_column($data, 0))) {
$result[] = sprintf(
'%s-%s: %s',
$data[0][1],
array_slice($data, -1)[0][1],
$time
);
} else {
foreach ($data as list(, $day)) {
$result[] = sprintf('%s: %s', $day, $time);
}
}
}
return join(', ', $result);
}
foreach ($inputs as $input) {
echo format_day_times($input), PHP_EOL;
}
Пн-Пт: 08:00 - 20:00, Сб: 08:00 - 18:00, Вс: 10:00 - 15:00
Пн-Вт: 08:00 - 19:00, Ср: 08:00 - 21:00, Чт-Пт: 08:00 - 20:00, Сб: 08:00 - 18:00, Вс: 10:00 - 15:00