这是我从服务器检索的数组:
Array ( [0] => Array ( [IdEvent] => 5 [EventName] => Copy of Opening snow weekend [Year] => 2012 [Month] => 08 ) [1] => Array ( [IdEvent] => 7 [EventName] => Copy of Copy of Opening snow weekend [Year] => 2012 [Month] => 07 ) [2] => Array ( [IdEvent] => 1 [EventName] => Opening snow weekend [Year] => 2011 [Month] => 05 ) [3] => Array ( [IdEvent] => 6 [EventName] => Copy of Opening snow weekend [Year] => 2010 [Month] => 05 ) )
这是我需要的结果数组
Array ( [2012] => Array ( [08] => Array ( [IdEvent] => 5 [EventName] => Copy of Opening snow weekend ) [07] => Array ( [IdEvent] => 7 [EventName] => Copy of Opening snow weekend ) ) [2011] => Array ( [05] => Array ( [IdEvent] => 1 [EventName] => Copy of Opening snow weekend ) ) [2010] => Array ( [05] => Array ( [IdEvent] => 6 [EventName] => Copy of Opening snow weekend ) ) )
我设法使用以下代码按年份对它们进行分组:
foreach($query->result_array() as $event){
if(isset($data[$event['year']])){
$data[$event['year']][] = $event['event'];
} else {
$data[$event['year']] = array($event['event']);
}
}
答案 0 :(得分:2)
尝试
foreach($query->result_array() as $event){
if(isset($event['year'])){
$data[$event['year']][$event['Month']]['IdEvent'] = $event['IdEvent'];
$data[$event['year']][$event['Month']]['EventName'] = $event['EventName'];
} else {
$data[$event['year']] = array($event['event']);
}
}
甚至你可以试试
if(isset($event['year'])){
$data[$event['year']][$event['Month']] = array( 'IdEvent' => $event['IdEvent'],
'EventName' => $event['EventName']);
}