我有一个名为$events
的关联数组,如下所示(以下是print_r($events);
的输出):
注意:实际数组的大小非常大。出于演示的目的,我从中选取了一些元素。
Array
(
[0] => Array
(
[group_name] =>
[event_id] => 201
[module_id] => event
[user_id] => 991
[title] => Zenda Vandan
[location] => Satara, Maharashtra, India
[country_iso] => MS
[country_child_id] => 0
[postal_code] =>
[city] =>
[time_stamp] => 1428315119 // 6 April 2015
)
[1] => Array
(
[group_name] =>
[event_id] => 235
[module_id] => event
[user_id] => 901
[title] => Bootstrap Feed Page
[location] => Pune, Maharashtra, India
[country_iso] => MS
[country_child_id] => 0
[postal_code] =>
[city] =>
[time_stamp] => 1436856285 // 14 July 2015
)
[2] => Array
(
[rsvp_id] => 0
[event_id] => 236
[module_id] => event
[user_id] => 901
[title] => Multiple Events
[location] => Pune, Maharashtra, India
[country_iso] => MS
[country_child_id] => 0
[postal_code] =>
[city] =>
[time_stamp] => 1436856356 // 14 July 2015
)
[3] => Array
(
[rsvp_id] => 0
[event_id] => 237
[module_id] => event
[user_id] => 901
[title] => Many Events
[location] => Sangli, Maharashtra, India
[country_iso] => MS
[country_child_id] => 0
[postal_code] =>
[city] =>
[time_stamp] => 1436856356 // 14 July 2015
)
)
我想分组所有数组元素的日期明智,即从时间戳值的事件在同一日期应该分组在一起。下面应该是上述情况下所需的结果数组。
Array
(
['14 Jul, Tuesday'] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 235
[module_id] => event
[user_id] => 901
[title] => Bootstrap Feed Page
[location] => Pune, Maharashtra, India
[country_iso] => MS
[country_child_id] => 0
[postal_code] =>
[city] =>
[time_stamp] => 1436856285 // 14 July 2015
)
[1] => Array
(
[rsvp_id] => 0
[event_id] => 236
[module_id] => event
[user_id] => 901
[title] => Multiple Events
[location] => Pune, Maharashtra, India
[country_iso] => MS
[country_child_id] => 0
[postal_code] =>
[city] =>
[time_stamp] => 1436856356 // 14 July 2015
)
[2] => Array
(
[rsvp_id] => 0
[event_id] => 237
[module_id] => event
[user_id] => 901
[title] => Many Events
[location] => Sangli, Maharashtra, India
[country_iso] => MS
[country_child_id] => 0
[postal_code] =>
[city] =>
[time_stamp] => 1436856356 // 14 July 2015
)
)
['14 Apr, Monday'] => Array
(
[0] => Array
(
[group_name] =>
[event_id] => 201
[module_id] => event
[user_id] => 991
[title] => Zenda Vandan
[location] => Satara, Maharashtra, India
[country_iso] => MS
[country_child_id] => 0
[postal_code] =>
[city] =>
[time_stamp] => 1428315119 // 6 April 2015
)
)
)
我应该如何以最优的方式得到它?
非常直接…
$result = [];
foreach($events as $event){
//I included year in the date format so you years dont all end up in the same sub array
$result[date('d M, l Y',$event['time_stamp'])][]=$event;
}
print_r($result);