你好我有一个数组如下。
$testArray = array ( '12 AM - 01 AM', '01 AM - 02 AM', '02 AM - 03 AM', '03 AM - 04 AM', '04 AM - 05 AM', '06 AM - 07 AM', '07 AM - 08 AM', '08 AM - 09 AM', '09 AM - 10 AM', '10 AM - 11 AM', '11 AM - 12 PM', '12 PM - 01 PM', '01 PM - 03 PM', '03 PM - 04 PM' );
现在我只想将字符串显示为
12 AM -05 AM,06 AM - 04 PM
请小小的提示或帮助可以节省我的时间。 感谢名单...
答案 0 :(得分:1)
这是一些输出的工作示例代码:
12 AM - 05 AM, 06 AM - 04 PM
您可以使用此功能,也可以根据需要对其进行编辑:
<?php
$testArray = array
(
'12 AM - 01 AM',
'01 AM - 02 AM',
'02 AM - 03 AM',
'03 AM - 04 AM',
'04 AM - 05 AM',
'06 AM - 07 AM',
'07 AM - 08 AM',
'08 AM - 09 AM',
'09 AM - 10 AM',
'10 AM - 11 AM',
'11 AM - 12 PM',
'12 PM - 01 PM',
'01 PM - 03 PM',
'03 PM - 04 PM'
);
$now = time();
foreach($testArray as $trange){
$matches = array();
preg_match_all('/(\d+ (?:A|P)M) - (\d+ (?:A|P)M)/', $trange, $matches);
if(!isset($start)){
$start = $matches[1][0];
$end = $matches[2][0];
}else if(strtotime($end, $now) === strtotime($matches[1][0], $now))
$end = $matches[2][0];
else{ // End time not equal to previous start time
echo $start , ' - ' , $end, ', ';
$start = $matches[1][0];
$end = $matches[2][0];
}
}
echo $start , ' - ' , $end;
?>
答案 1 :(得分:0)
Using string functions:
$temp = explode('-',$testArray[0]);
$start_str = $temp[0];
$comma = '';
for ($i = 0; $i <= count($testArray); $i++) {
$temp = explode('-',$testArray[$i]);
$prev_start = $temp[0];
$prev_end = $temp[1];
$prev_endtime = (integer) $prev_end;
$temp = explode('-',$testArray[$i+1]);
$start = $temp[0];
$end = $temp[1];
$starttime = (integer) $start;
if($prev_endtime != $starttime){
$op .= " $comma $start_str - $prev_end";
$comma = ',';
$start_str = $start;
}
}
echo $op;