2013-05-01 6 views
0

가 나는 foreach 루프가이에서 정보의지고 있습니다중첩 PHP는 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  
     ); 

배열의 각 반복의 경우를, 그것은 제목, 설명을 보유하고 이벤트 DIV, 그리고에 대한 링크를 뱉어 실제 이벤트 페이지. 문제는 같은 날에 두 개의 이벤트가있는 경우 해당 날짜에 각 이벤트에 대해 두 개의 별도 div가 생성된다는 것입니다. 내가하고 싶은 것은 그들이 같은 날에있는 경우 같은 div 안에 이벤트를 넣는 것입니다.

나는 두 번째 foreach 루프를 중첩시켜야한다고 생각하지만, 그렇게 할 때 오류가 발생합니다. 여기

내가 노력하고있어, 난 잘못 알고,하지만 난 붙어 :

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

      } 


    } 

가 어떻게 하루에 하나 더 후이있는 경우 일이 함께 참여합니까?

+0

오류는 어떻게 생깁니 까? 그것은 당신에게 좋은 대답을주는 것이 유용 할 것입니다! – Ivo

답변

0

& 입력시 해결되지 않는 이유는 무엇입니까? 즉,

 $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  
    ); 
0

이 작업을 수행하는 간단한 방법은 있지만, 두 foreach 루프, 다른 후 하나, 중첩 foreach하지입니다. 첫 번째 이벤트에서는 그날의 이벤트를 새로운 배열에 넣고, 두 번째 이벤트는 해당 배열을 출력합니다.

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

몇 가지 서식이 필요하지만 아이디어가 있습니다.