Php foreach循环中的最后一项是它正在遍历的数组
我有一系列本周、下周和下周的日期,包括日期、开始时间和结束时间。出于某种原因,当foreach循环到达最终日期时,它会输出最后一周的数组,而不是最终日期。以下是阵列的开始:Php foreach循环中的最后一项是它正在遍历的数组,php,arrays,foreach,output,Php,Arrays,Foreach,Output,我有一系列本周、下周和下周的日期,包括日期、开始时间和结束时间。出于某种原因,当foreach循环到达最终日期时,它会输出最后一周的数组,而不是最终日期。以下是阵列的开始: $dates = Array ( [0] => Array ( [0] => Array ( [0] => "Mon 23rd June" [1]
$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为$week)
{
如果($i==2)
{
$html.='';
}
其他的
{
$html.='';
}
$html.='
开始的一周“$Week[0][0]”
白天
打开
关闭
';
foreach($周为$日)
{
$html.='
“.$day[0]”
“.$day[1]”
“.$day[2]”
';
}
$html.='';
++$i;
}
谁能看出发生了什么事
编辑
我发现$dates是可以的,问题发生在foreach($dates as$week)循环在上周运行时
重新编辑
这是它的函数。请不要评判,我继承了这个网站: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;
}
函数getOpeningHours(){
日期默认时区设置(“欧洲/伦敦”);
$dates=数组(
排列(
排列(
日期(“本周星期一”),
“上午9点”,
“晚上7:00”
),
排列(
日期(“本周星期二”),
“上午9点”,
“晚上7:00”
),
排列(
日期(“本周星期三”),
“上午9点”,
“晚上7:00”
),
排列(
日期(“本周星期四”),
“上午9点”,
“晚上7:00”
),
排列(
日期(“本周星期五”),
“上午9点”,
“晚上7:00”
),
排列(
日期(“本周星期六”),
“上午9点”,
“晚上7:00”
),
排列(
日期(“D jS F”,标准时间(“本周星期日”),
“上午9点”,
“晚上7:00”
),
),
排列(
排列(
日期(“D jS F”,标准时间(“下周星期一”),
“上午9点”,
“晚上7:00”
),
排列(
日期(星期二),标准时间(下星期二),
“上午9点”,
“晚上7:00”
),
排列(
日期(星期三),标准时间(下星期三),
“上午9点”,
“晚上7:00”
),
排列(
日期(星期四),标准时间(下星期四),
“上午9点”,
“晚上7:00”
),
排列(
日期(“D jS F”,标准时间(“下星期五”),
“上午9点”,
“晚上7:00”
),
排列(
日期(“D jS F”,标准时间(“下星期六”),
“上午9点”,
“晚上7:00”
),
排列(
日期(星期日),标准时间(下星期日),
“上午9点”,
“晚上7:00”
),
),
排列(
排列(
日期(“D jS F”,标准时间(“下周星期一”,标准时间(“下周星期一”),
“上午9点”,
“晚上7:00”
),
排列(
日期(星期二),标准时间(下星期二),标准时间(下星期二),
“上午9点”,
“晚上7:00”
),
排列(
日期(“D jS F”,标准时间(“下星期三”,标准时间(“下星期三”),
“上午9点”,
“晚上7:00”
),
排列(
日期(“D jS F”,标准时间(“下星期四”,标准时间(“下星期四”),
“上午9点”,
“晚上7:00”
),
排列(
日期(“D jS F”,标准时间(“下星期五”,标准时间(“下星期五”),
“上午9点”,
“晚上7:00”
),
排列(
日期(“D jS F”,标准时间(“下星期六”,标准时间(“下星期六”),
“上午9点”,
“晚上7:00”
),
排列(
日期(“D jS F”,标准时间(“本周星期日”,标准时间(“下周星期日”),
“上午9点”,
“晚上7:00”
)
),
);
$sql[0]=“从'tbl_opening_exceptions'中选择*,其中'exception_date`>='”。date(“Y-m-d”,strotime(“本周星期一”)。”和'exception_date`='。date(“Y-m-d”,strotime(“下周星期一”)。”和'exception_date`='。date(“Y-m-d”,strotime(“下周星期一”),strotime(“下周星期一”)。”和'exception"好的
所以,如果你的目标是只打印“6月23日星期一”,试试看
另外,下次提到如何声明数组时,这会变得容易得多。我认为您的$weeks数组应该从外观上称为$week。而且,array(
date("D jS F", strtotime("monday next week", strtotime("monday next week"))),
"9:00am",
"7:00pm"
),
执行类似date(“D jS F”,strotime(“周一下周+7天”)。这将得到周一下周+7天,也就是两周后的周一。我不认为“周一下周”,strotime(“周一下周”)是有效的语法。同样的错误可能在这里:
$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")))."'";
将其更改为下周+7天,我认为可能会有一些好处。TL;DR-参考文献是邪恶的! 问题在于:
foreach ($dates[$i] as &$week) {
// updates based on database values
}
foreach ($dates as $week) {
// generate html from week data
}
在第一个循环完成后,最后一周仍然是一个引用,$dates[count($dates)-1]
。在第二个循环中,$week
依次分配$dates
的每个元素
当涉及到最后一个元素时,$week
被分配给它自己,从而创建一个递归结构
解决方法很简单:
foreach ($dates[$i] as &$week) {
// updates based on database values
}
unset($week); // remove the reference
或者:
foreach ($dates[$i] as $week) {
// updates based on database values
if (<some condition>) {
$dates[$i][1] = 'foo';
$dates[$i][2] = 'bar';
}
}
foreach($dates[$i]作为$week){
//基于数据库值的更新
如果(){
$dates[$i][1]=“foo”;
$dates[$i][2]=“bar”;
}
}
在t的末尾有一个额外的
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"
)
foreach ($dates[$i] as &$week) {
// updates based on database values
}
foreach ($dates as $week) {
// generate html from week data
}
foreach ($dates[$i] as &$week) {
// updates based on database values
}
unset($week); // remove the reference
foreach ($dates[$i] as $week) {
// updates based on database values
if (<some condition>) {
$dates[$i][1] = 'foo';
$dates[$i][2] = 'bar';
}
}