我有一个代码,我可以在这里与商店的营业时间相似。例如,如果我们有:
<?php
$openHours = array(
'Mon' => '9am-7pm',
'Tue' => '9am-7pm',
'Wed' => '9am-7pm',
'Thu' => '9am-6pm',
'Fri' => 'closed',
'Sat' => '9am-10pm',
'Sun' => '9am-10pm'
);
foreach($openHours as $k => $v)
$array[$v][] = $k;
foreach($array as $time => $days)
echo implode(",", $days) . " " . $time . PHP_EOL;
?>
结果如下所示。
Mon,Tue,Wed 9am-7pm
Thu 9am-6pm
Fri closed
Sat,Sun 9am-10pm
我想要什么?
如果天数是连续的,我想连字符,如果不是连续的例子,我想添加逗号。
结果应该是这样的
Mon-Wed 9am-7pm // Hyphenate as days are consecutive and more than 2
Thu 9am-6pm
Fri closed
Sat,Sun 9am-10pm // Adds comma as there are only 2
答案 0 :(得分:2)
试试这个。
更新2
<?php
$openHours = array(
'Mon' => '9am-7pm',
'Tue' => '9am-7pm',
'Wed' => '9am-7pm',
'Thu' => '9am-10pm',
'Fri' => 'closed',
'Sat' => '9am-10pm',
'Sun' => '9am-10pm'
);
$new_array = array();
foreach($openHours as $key => $value)
{
if(in_array($value,$new_array))
{
$key_new = array_search($value, $new_array);//to get the key of element
unset($new_array[$key_new]); //remove the element
$key_new = $key_new.','.$key; //updating the key
$new_array[$key_new] = $value; //inserting new element to the key
}
else
{
$new_array[$key] = $value;
}
}
foreach ($new_array as $days=>$time){
$daylist = explode(',',$days);
if ($time!='closed'){
if (count($daylist)>2){
$limit = count($daylist)-1;
$first = $daylist[0];
$last = $daylist[$limit];
//loop will go here.
if (date('D', strtotime('+'.$limit.' days', strtotime($first)))==$last){
echo $first.'-'.$last.' '.$time.'<br>';
} else {
$sep = '';
foreach ($daylist as $sepdays){
echo $sep.$sepdays;
$sep = ',';
}
echo ' '.$time.'<br>';
}
} else {
echo $days.' '.$time.'<br>';
}
} else {
$daylist = explode(',',$days);
foreach ($daylist as $sepdays){
echo $sepdays.' '.$time.'<br>';
}
}
}
?>
<强> RESULT 强>
Mon-Wed 9am-7pm
Fri closed
Thu,Sat,Sun 9am-10pm
答案 1 :(得分:1)
您可以这样使用:
$openHours = array(
'Mon' => '9am-7pm',
'Tue' => '9am-7pm',
'Wed' => '9am-7pm',
'Thu' => '9am-6pm',
'Fri' => 'closed',
'Sat' => '9am-10pm',
'Sun' => '9am-10pm'
);
foreach($openHours as $k => $v){
$array[$v][] = $k;
}
foreach($array as $time => $days){
if(count($days) > 2){ // if count more than two days
echo current($days)."-".end($days) . " " . $time . PHP_EOL."<br/>";
}
else{ // if normal like one or two days
echo implode(",", $days) . " " . $time . PHP_EOL."<br/>";
}
}
<强>结果:强>
Mon-Wed 9am-7pm
Thu 9am-6pm
Fri closed
Sat,Sun 9am-10pm
旁注:
如果您想将Sat,Sun
用作Sat-Sun
,而不仅仅使用if(count($days) > 1){
条件。
更新1:
示例,如果Fri
和Tue
已关闭,则可以使用。
<?php
$openHours = array(
'Mon' => '9am-7pm',
'Tue' => 'closed',
'Wed' => '9am-7pm',
'Thu' => '9am-6pm',
'Fri' => 'closed',
'Sat' => '9am-10pm',
'Sun' => '9am-10pm'
);
foreach($openHours as $k => $v){
$array[$v][] = $k;
}
foreach($array as $time => $days){
if(count($days) > 2 && $time != 'closed'){ // if count more than two days
echo current($days)."-".end($days) . " " . $time . PHP_EOL."<br/>";
}
elseif($time == 'closed'){ // if count more than two days
echo implode(" closed<br/>", $days) . " " . $time . PHP_EOL."<br/>";
}
else{ // if normal like one or two days
echo implode(",", $days) . " " . $time . PHP_EOL."<br/>";
}
}
?>
<强>结果:强>
Mon,Wed 9am-7pm
Tue closed
Fri closed
Thu 9am-6pm
Sat,Sun 9am-10pm