如何根据特定键(本例中为日期)的值对内部数组中的数组元素进行分组?
How to group array elements in inner arrays based on a value from specific key(date in this case)?
我有一个名为 $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);
我有一个名为 $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);