繁体   English   中英

foreach循环中的最后一项是它遍历的数组

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM