尝试从foreach循环中的值创建一个字符串,确实没有更好的方法来执行此操作,但它没有设置字符串。我无法重置它以清空每一次迭代,因为我需要它继续,我已经阅读了重复的问题,他们没有回答为什么它不起作用,任何帮助表示赞赏< / p>
这个foreach只是循环检查并检查具有多个小时的企业的营业时间格式
$hoursSpliced = ['5:00 AM - 11:00 AM', '5:00 PM - 2:00 AM']
$hoursFinal = '';
foreach($hoursSpliced as $hour) {
$hours = explode(' - ', $hour, 2);
//If the hours match the format, we continue onward, no need to touch those
if(preg_match($regex, $hour)) {
$hoursFinal = $hoursFinal . $hour;
continue;
}
//0 stores opening hours, 1 stores closing hours
//Lower them so we can compare
$hours[0] = strtolower(trim($hours[0]));
$hours[1] = strtolower(trim($hours[1]));
//If it's in the AM we add it to the end, same goes for PM, if there isn't one we assume it's PM
if(strpos($hours[0], 'am') !== false) {
$hoursOpen = array_map('trim', explode('am', $hours[0]));
$hoursOpen[1] = 'am';
} elseif(strpos($hours[0], 'pm') !== false) {
$hoursOpen = array_map('trim', explode('pm', $hours[0]));
$hoursOpen[1] = 'pm';
} else {
$hoursOpen = [$hours[0]];
$hoursOpen[] = 'pm';
}
//Same thing as above for closing hours
if(strpos($hours[1], 'am') !== false) {
$hoursClosed = array_map('trim', explode('am', $hours[1]));
$hoursClosed[1] = 'am';
} elseif(strpos($hours[1], 'pm') !== false) {
$hoursClosed = array_map('trim', explode('pm', $hours[1]));
$hoursClosed[1] = 'pm';
} else {
$hoursClosed = [$hours[1]];
$hoursClosed[] = 'pm';
}
$hoursFinal .= ", $hoursOpen[0] $hoursOpen[1] - $hoursClosed[0] $hoursClosed[1]";
}
$hoursFinal = trim(', ', $hoursFinal);
dd($hoursFinal);
答案 0 :(得分:0)
知道了,花了我一会儿 你向后修剪了
$hoursFinal = trim(', ', $hoursFinal);
应该是
$hoursFinal = trim( $hoursFinal, ", ");