在fo​​reach循环的最后一项是它遍历数组 [英] Last item in foreach loop is the array it is traversing

查看:169
本文介绍了在fo​​reach循环的最后一项是它遍历数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个星期,下周中的控股之日起,一开场时间和结束时间日期后的数组和星期。出于某种原因,当foreach循环到达最后期限,它输出的最后一周,而不是最后日期的数组。这里是数组的启动:

  $日期=阵列

    [0] =>排列
        (
            [0] =>排列
                (
                    [0] => 周一6月23日
                    [1] => 9:00 AM
                    [2] => 下午7时00
                )            [1] =>排列
                (
                    [0] => 周二6月24日
                    [1] => 9:00 AM
                    [2] => 下午7时00
                )
            ...
    [1] =>排列
        (            [0] =>排列
                (
                    [0] => 周一6月30日
                    [1] => 9:00 AM
                    [2] => 下午7时00
                )            [1] =>排列
                (
                    [0] => 周二7月1日
                    [1] => 9:00 AM
                    [2] => 下午7时00
                )
            ...

有与无阵的问题,因为我已经打印出来。下面是foreach循环(它是嵌套的foreach($一周$日)发生错误):

 的foreach($日期为$周)
{
    如果($ I == 2)
    {
        $的HTML ='<表格的cellpadding =5CELLSPACING =2BORDER =0级=kc_ot_openingTable最后一个>'。
    }
    其他
    {
        $的HTML ='<表格的cellpadding =5CELLSPACING =2BORDER =0级=kc_ot_openingTable>'。
    }
    $的HTML ='< TR类=kc_ot_weekCommence>
        &所述; TD列跨度=3>周开始'。$周[0] [0]。&下; / TD>
    < / TR>
    &所述; TR类=kc_ot_openingTableTitle>
        < TD类=天>白天< / TD>
        < TD类=开放>开放< / TD>
        < TD类=封闭&GT封闭的LT; / TD>
    < / TR>';    的foreach($一周$日)
    {
        $的HTML ='< TR>
        < TD>&LT'$天[0]。'; / TD>
        &所述; TD类=开>'。$天[1]。&下; / TD>
        &所述; TD类=封闭>'。$天[2]。&下; / TD>
    < / TR>
    &所述; TR>';
    }
    $ HTML ='< /表>';
    ++ $ I;
}

任何人都可以找出到底是怎么回事?

修改

我已经发现,$日期是好的,当在foreach($日期为$周)环上的最后一周运行出现问题。

再修改

下面的是,这来自于功能。请不要判断,我继承了这个网站:P

 函数getOpeningHours(){date_default_timezone_set(欧洲/伦敦);
$日期=阵列(
    阵列(
        阵列(
            日期(D JS F的strtotime(本周星期一))
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(本周星期二))
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(本周日)),
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(本周星期四))
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(本周星期五))
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(本周星期六))
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(本周星期天))
            9:00 AM,
            下午7时00
        )
    )
    阵列(
        阵列(
            日期(D JS F的strtotime(下周一)),
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(在下周二)),
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(「WEDNESDAY下周)),
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(周四下周)),
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(星期五下周)),
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(下周六)),
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(星期日下周)),
            9:00 AM,
            下午7时00
        )
    )
    阵列(
        阵列(
            日期(D JS F,(下周一的strtotime,的strtotime(下周一)))
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(在下周二的strtotime(在下周二)))
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(周三下周的strtotime(周三下周)))
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(周四下周的strtotime(周四下周)))
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F,(星期五下周的strtotime,的strtotime(星期五下周)))
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F,(下周六的strtotime,的strtotime(下周六)))
            9:00 AM,
            下午7时00
        )
        阵列(
            日期(D JS F的strtotime(本周星期天,的strtotime(星期日下周)))
            9:00 AM,
            下午7时00
        )
    )
);$ SQL [0] =SELECT * FROM`tbl_opening_exceptions` WHERE`exception_date`> ='.date(年月日的strtotime(本周星期一))。'和'exception_date`< =' .date(年月日的strtotime(本周星期天))'。
$ SQL [1] =SELECT * FROM`tbl_opening_exceptions` WHERE`exception_date`> ='.date(年月日的strtotime(下周一))。'和'exception_date`< =' .date(年月日的strtotime(星期日下周))'。
$ SQL [2] =SELECT * FROM`tbl_opening_exceptions` WHERE`exception_date`> ='((下周一))年月日,(下周一的strtotime,的strtotime).date。和`exception_date`< ='.date(年月日,(的strtotime星期日下周的strtotime(星期日下周)))。';
$ I = 0;
的foreach($ sql作为$字符串)
{
    $结果= mysql_query($字符串)或死亡(mysql_error());
    $ R = mysql_fetch_array($结果);    的foreach($日期[$ i]为&放大器; $周)
    {
        如果($星期[0] ==日期(D JS F的strtotime($ R [exception_date])))
        {
            $星期[1] =日期(G:IA的strtotime($ R [exception_opening]));
            每周$ [2] =日期(G:IA的strtotime($ R [exception_closing]));
        }
    }
    ++ $ I;
}
$的HTML =;
$ I = 0;
//的print_r($日期);
的foreach($日期为$周)
{
的print_r($周);
    如果($ I == 2)
    {
        $的HTML ='<表格的cellpadding =5CELLSPACING =2BORDER =0级=kc_ot_openingTable最后一个>'。
    }
    其他
    {
        $的HTML ='<表格的cellpadding =5CELLSPACING =2BORDER =0级=kc_ot_openingTable>'。
    }
    $的HTML ='< TR类=kc_ot_weekCommence>
        &所述; TD列跨度=3>周开始'。$周[0] [0]。&下; / TD>
    < / TR>
    &所述; TR类=kc_ot_openingTableTitle>
        < TD类=天>白天< / TD>
        < TD类=开放>开放< / TD>
        < TD类=封闭&GT封闭的LT; / TD>
    < / TR>';    的foreach($一周$日)
    {
        $的HTML ='< TR>
        < TD>&LT'$天[0]。'; / TD>
        &所述; TD类=开>'。$天[1]。&下; / TD>
        &所述; TD类=封闭>'。$天[2]。&下; / TD>
        < / TR>';
    }
    $ HTML ='< /表>';
    ++ $ I;
}
返回$ HTML;
}


解决方案

TL;!DR - 参考文献是邪恶

问题就出在这里:

 的foreach($日期[$ i]为&放大器; $周){
    //基于数据库值更新
}的foreach($日期为$周){
    //生成周的数据的HTML
}

第一个循环结束后,上周仍然是一个参考,所以是 $日期[计数($日期) - 1] 。里面的第二个循环, $一周被分配 $日期中的每个元素依次

在谈到最后一个元素, $一周被分配给自己,从而递归结构被创建。

解决方法是简单的:

 的foreach($日期[$ i]为&放大器; $周){
    //基于数据库值更新
}
未设置($周); //删除引用

或者

 的foreach($日期[$ i]为$周){
    //基于数据库值更新
    如果(小于某些条件>){
        $日期[$ i] [1] ='富';
        $日期[$ i] [2] ='吧';
    }
}

I have an array of this weeks, next weeks and the week after's dates holding the date, an opening time and a closing time. For some reason, when the foreach loop reaches the final date, it outputs the array of the final week instead of the final date. Here is the start of the array:

$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"
                )
            ...

There is no issue with the array, as I have printed it out. Here is the foreach loop (it is the nested foreach ($week as $day) where the error occurs):

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;
}

Can anyone spot what is going on?

Edit

I have found out that $dates is fine, the problem occurs when the foreach($dates as $week) loop runs on the last week.

Re Edit

Here is the function that this comes from. Please don't judge, I inherited this site :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 - References are evil!

The problem lies here:

foreach ($dates[$i] as &$week) {
    // updates based on database values
}

foreach ($dates as $week) {
    // generate html from week data
}

After the first loop finishes, the last week is still a reference and so is $dates[count($dates) - 1]. Inside the second loop, $week gets assigned each element of $dates in turn.

When it comes to the last element, $week gets assigned to itself and thus a recursive structure is created.

The fix is simple:

foreach ($dates[$i] as &$week) {
    // updates based on database values
}
unset($week); // remove the reference

Alternatively:

foreach ($dates[$i] as $week) {
    // updates based on database values
    if (<some condition>) {
        $dates[$i][1] = 'foo';
        $dates[$i][2] = 'bar';
    }
}

这篇关于在fo​​reach循环的最后一项是它遍历数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆