【发布时间】:2014-08-19 04:30:17
【问题描述】:
我有一组包含日期、开始时间和结束时间的本周、下周和下周的日期。出于某种原因,当 foreach 循环到达最终日期时,它会输出最后一周的数组而不是最终日期。这是数组的开头:
$dates = Array
(
[0] => Array
(
[0] => Array
(
[0] => "Mon 23rd June"
[1] => "9:00am"
[2] => "7:00pm"
)
[1] => Array
(
[0] => "Tue 24th June"
[1] => "9:00am"
[2] => "7:00pm"
)
...
[1] => Array
(
[0] => Array
(
[0] => "Mon 30th June"
[1] => "9:00am"
[2] => "7:00pm"
)
[1] => Array
(
[0] => "Tue 1st July"
[1] => "9:00am"
[2] => "7:00pm"
)
...
数组没有问题,因为我已经打印出来了。这是foreach循环(它是发生错误的嵌套foreach($week as $day)):
foreach($dates as $week)
{
if($i == 2)
{
$html .= '<table cellpadding="5" cellspacing="2" border="0" class="kc_ot_openingTable last">';
}
else
{
$html .= '<table cellpadding="5" cellspacing="2" border="0" class="kc_ot_openingTable">';
}
$html.= '<tr class="kc_ot_weekCommence">
<td colspan="3">Week Commencing '.$week[0][0].'</td>
</tr>
<tr class="kc_ot_openingTableTitle">
<td class="day">Day</td>
<td class="open">Open</td>
<td class="closed">Closed</td>
</tr>';
foreach($week as $day)
{
$html .= '<tr>
<td>'.$day[0].'</td>
<td class="open">'.$day[1].'</td>
<td class="closed">'.$day[2].'</td>
</tr>
<tr>';
}
$html .= '</table>';
++$i;
}
谁能看出发生了什么?
编辑
我发现 $dates 没问题,问题是在上周运行 foreach($dates as $week) 循环时出现的。
重新编辑
这是 this 来自的函数。请不要判断,我继承了这个网站:P
function getOpeningHours() {
date_default_timezone_set('Europe/London');
$dates = array(
array(
array(
date("D jS F", strtotime("monday this week")),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("tuesday this week")),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("wednesday this week")),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("thursday this week")),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("friday this week")),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("saturday this week")),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("sunday this week")),
"9:00am",
"7:00pm"
),
),
array(
array(
date("D jS F", strtotime("monday next week")),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("tuesday next week")),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("wednesday next week")),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("thursday next week")),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("friday next week")),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("saturday next week")),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("sunday next week")),
"9:00am",
"7:00pm"
),
),
array(
array(
date("D jS F", strtotime("monday next week", strtotime("monday next week"))),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("tuesday next week", strtotime("tuesday next week"))),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("wednesday next week", strtotime("wednesday next week"))),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("thursday next week", strtotime("thursday next week"))),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("friday next week", strtotime("friday next week"))),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("saturday next week", strtotime("saturday next week"))),
"9:00am",
"7:00pm"
),
array(
date("D jS F", strtotime("sunday this week", strtotime("sunday next week"))),
"9:00am",
"7:00pm"
)
),
);
$sql[0] = "SELECT * FROM `tbl_opening_exceptions` WHERE `exception_date` >= '".date("Y-m-d", strtotime("monday this week"))."' AND `exception_date` <= '".date("Y-m-d", strtotime("sunday this week"))."'";
$sql[1] = "SELECT * FROM `tbl_opening_exceptions` WHERE `exception_date` >= '".date("Y-m-d", strtotime("monday next week"))."' AND `exception_date` <= '".date("Y-m-d", strtotime("sunday next week"))."'";
$sql[2] = "SELECT * FROM `tbl_opening_exceptions` WHERE `exception_date` >= '".date("Y-m-d", strtotime("monday next week", strtotime("monday next week")))."' AND `exception_date` <= '".date("Y-m-d", strtotime("sunday next week", strtotime("sunday next week")))."'";
$i=0;
foreach($sql as $string)
{
$result = mysql_query($string) or die(mysql_error());
$r = mysql_fetch_array($result);
foreach($dates[$i] as &$week)
{
if($week[0] == date("D jS F", strtotime($r["exception_date"])))
{
$week[1] = date("g:ia", strtotime($r["exception_opening"]));
$week[2] = date("g:ia", strtotime($r["exception_closing"]));
}
}
++$i;
}
$html = "";
$i = 0;
//print_r($dates);
foreach($dates as $week)
{
print_r($week);
if($i == 2)
{
$html .= '<table cellpadding="5" cellspacing="2" border="0" class="kc_ot_openingTable last">';
}
else
{
$html .= '<table cellpadding="5" cellspacing="2" border="0" class="kc_ot_openingTable">';
}
$html.= '<tr class="kc_ot_weekCommence">
<td colspan="3">Week Commencing '.$week[0][0].'</td>
</tr>
<tr class="kc_ot_openingTableTitle">
<td class="day">Day</td>
<td class="open">Open</td>
<td class="closed">Closed</td>
</tr>';
foreach($week as $day)
{
$html .= '<tr>
<td>'.$day[0].'</td>
<td class="open">'.$day[1].'</td>
<td class="closed">'.$day[2].'</td>
</tr>';
}
$html .= '</table>';
++$i;
}
return $html;
}
【问题讨论】:
-
您在内部
foreach的HTML 末尾有一个额外的<tr>。 -
澄清一下,3 个表格中的每一个都将最后一行打印两次?还是只是最后一张桌子?
-
$dates[2][6] 中的最后一个星期日包含 $dates[2] 数组,当我查看它的 print_r() 时,它会显示 recursion。输入到 foreach($dates as $week) 的数组很好,当 foreach 运行时会发生错误。
-
不影响报错。所以卡住了!
-
@PhilYoung 找到了,邪恶的引用:)