dongxingdu9594 2015-07-16 13:44
浏览 18
已采纳

如何根据特定键的值(在本例中为日期)对内部数组中的数组元素进行分组?

I've an associative array called $events as follows (following is the output of print_r($events);) :

Note : The actual array is very large in size. For demonstration purpose I've put few elements from it.

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
         )
)

I want to group all array elements date-wise i.e. from time stamp values the events coming on same date should be grouped together. Following should be the desired resultant array in above case.

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
                )
            )

)

How should I get this in an optimum way?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • douzhanjia0773 2015-07-16 13:53
    关注

    Pretty straight forward...

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动