0

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 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.