简体   繁体   English

嵌套的PHP foreach循环

[英]Nested php foreach loop

I have a foreach loop getting it's info from this: 我有一个foreach循环从这里获取它的信息:

        $eventarray[] = array(          
            "month" => $cal_months[$event_month],           
            "day1" => $event_day1,             
            "title" => $title,            
            "desc" => html_entity_decode($article),
            "month_link" =>   strtolower($event_month),
            "link" => $event_link      
        ); 

For each iteration of the array, it spits out an event div that holds the title, description, and a link to the actual event page. 对于数组的每次迭代,它都会弹出一个事件div,该事件div包含标题,描述以及指向实际事件页面的链接。 The problem with this is is that if there is are two events on the same day, I get two separate divs for each event on that day. 问题是,如果同一天有两个事件,那么我在当天的每个事件中都会得到两个单独的div。 What I would like to do is put the events within the same div if they are on the same day. 我想做的是将事件在同一天放入同一div中。

I "think" I have to nest a second foreach loop, but when I do that it error's out. 我“认为”我必须嵌套第二个foreach循环,但是这样做时出错了。

Here's what I'm trying, and I know it's wrong, but I'm stuck: 这是我正在尝试的方法,我知道这是错误的,但我遇到了麻烦:

foreach($eventarray as $value){

        if($value['month'] == $thismonth){

            $day[] = $value['day1'];

            echo $value['title'];
            echo $value['desc'];
            echo $value['link'];
            foreach($day as $day_value){
                echo 'test';

            }


    }

How do I get the days to join together if there are more then one on a single day? 如果一天中有不止一天,我如何才能在一起?

Why dont you try & solve on the input. 您为什么不尝试并解决输入问题。 Ie

     $eventarray[$event_day1][] = array(          
        "month" => $cal_months[$event_month],           
        "day1" => $event_day1,             
        "title" => $title,            
        "desc" => html_entity_decode($article),
        "month_link" =>   strtolower($event_month),
        "link" => $event_link      
    ); 

The simple way to do this is not with a nested foreach , but with two foreach loops, one after the other. 执行此操作的简单方法不是使用嵌套的foreach ,而是使用两个foreach循环,一个接一个地循环。 In the first one, put the events for the day into a new array, and in the second one, print that array. 在第一个中,将当天的事件放入一个新数组,在第二个中,打印该数组。

// This will actually be a 2-dimensional array
$events_by_day = array();

// Get this month's events and group by day.
foreach($eventarray as $value){
    if($value['month'] == $thismonth){
        // Push this event into the $events_by_day[<DAY>] array
        $events_by_day[$value['day1']][] = $value;
    }
}

// For each day, print it.
foreach($events_by_day as $day => $events_today){
    if (count($events_today) > 0){
        echo '<div>';
        echo "$month $day";
        // Get today's events
        foreach($events_today as $event){
            echo $event['title'];
            echo $event['desc'];
            echo $event['link'];
        }
        echo '</div>';
    }
}

It needs some formatting, but you get the idea. 它需要一些格式,但是您明白了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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