请注意,我的输入与您的不同...
<?php
$times =
array (
'10:00-18:00' =>
array (
0 => 'Mon',
1 => 'Tues',
2 => 'Wed',
3 => 'Fri',
4 => 'Sat',
),
'10:00-21:00' =>
array (
3 => 'Thu',
),
'11:00-21:00' =>
array (
0 => 'Tues',
1 => 'Wed',
2 => 'Thu',
),
'10:00-13:00' =>
array (
0 => 'Sun',
1 => 'Mon',
),
);
$info = array();
foreach ($times as $time => $days)
{
$days = process_days($days);
$info[] = $time.": ".implode(', ',$days);
}
print_r($info);
/*
Array
(
[0] => 10:00-18:00: Mon - Wed, Fri - Sat
[1] => 10:00-21:00:
[2] => 11:00-21:00: Tue - Thu
[3] => 10:00-13:00: Sun - Mon
)
*/
function process_days($days)
{
$days = order_the_days($days);
$return = array();
$end_day = end($days);
reset($days);
$consecutive = false;
$consecutive_days = array();
$current_day = key($days);
while ($days[$current_day] !== $end_day)
{
next($days);
$next_day = key($days);
if ($next_day == $current_day+1 || ($next_day==1 && $current_day=7))
{
$consecutive = true;
$consecutive_days[] = $current_day;
}
else // not consecutive
{
if ($consecutive===true)
{
$return[] = figure_out_consecutive_days($consecutive_days,$current_day,$days);
$consecutive_days = array();
}
else
{
$return[] = $days[$current_day];
}
$consecutive = false;
}
$current_day = key($days);
}
if ($consecutive) {
$return[] = figure_out_consecutive_days($consecutive_days,$current_day,$days);
}
return $return;
}
function figure_out_consecutive_days($consecutive_days,$current_day,$days)
{
$consecutive_days[$current_day] = $days[$current_day];
reset($consecutive_days);
$first = current($consecutive_days);
$end = end($consecutive_days);
return $days[$first]." - $end";
}
function order_the_days($days)
{
$return = array();
foreach ($days as $i => $day)
{
$day = substr($day,0,3);
$dow = date('N', strtotime($day));
$return[$dow] = $day;
}
return $return;
}
?>