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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记