[英]Last item in foreach loop is the array it is traversing
我有幾個星期,下個星期以及持有日期,開放時間和結束時間的一周后的一周。 出於某種原因,當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;
}
誰能發現發生了什么?
編輯
我發現$ date很好,當foreach($ date為$ 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;
}
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';
}
}
好的,
$weeks = array(
array(
array("Mon 23rd June", "9:00 AM", "7:00 PM"),
array("Tue 24th June", "9:00 AM", "7:00 PM"),
array("Wed 25th June", "9:00 AM", "7:00 PM") //etc
),
array() // another week
)
$dates = array($weeks)
因此,如果您的目標是打印“6月23日星期一”,請嘗試
print_r(day[0][0]);
此外,下次提到你如何聲明數組,這變得容易多了。 我認為你的$ weeks數組應該從它的外觀稱為$ week。 </ tr> <tr>似乎是無意的
編輯:我發現了錯誤
array(
date("D jS F", strtotime("monday next week", strtotime("monday next week"))),
"9:00am",
"7:00pm"
),
做一些像約會(“D jS F”,strtotime(“下周一周一+ 7天”)。這將是下周一周一+ 7天,也就是從現在起兩周后的周一。我不認為“”下周一周一“,strtotime(”下周星期一“)”是有效的語法。同樣的錯誤可能在這里:
$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")))."'";
只是建議更好地生成日期,但對於您的商店或其他任何方式可能是最方便的。 在PHP中創建(日期,值)數組/集合/列表使用此命令創建數組,如果必須將其拆分為幾周,請使用array_chunk [$ dates,7]
重新編輯:
看看那個日期數組的最后一部分
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"
)
將其更改為下周+ 7天,我認為它可能會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.