-2

I am having this array

 Array
    (
        [0] => Array
            (
                [meeting_archive_id] => 1
                [corp_aggregate_id] => 1
                [created_by] => 103
                [created_to] => 2
                [date] => 15-07-2016
                [time] => 10:00 am
                [entry_id] => 103
                [user_type] => 1
                [firstname] => Kenyon
                [lastname] => Martena
                [job_title] => 
                [email] => 
                [company_name] => Lorem Ipsum Sodales Incorporated
                [phone] => 1-446-161-3194
                [type] => meeting
            )

        [1] => Array
            (
                [meeting_archive_id] => 16
                [corp_aggregate_id] => 2
                [created_by] => 103
                [created_to] => 6
                [date] => 17-07-2016
                [time] => 1:00 am
                [entry_id] => 103
                [user_type] => 1
                [firstname] => Hedley
                [lastname] => Aurelia
                [job_title] => 
                [email] => 
                [company_name] => Sit Amet Ante Corp.
                [phone] => 1-484-144-8520
                [type] => meeting
            )

        [2] => Array
            (
                [user_type] => 2
                [firstname] => Abbot
                [lastname] => Odessa
                [job_title] => 
                [email] => 
                [company_name] => Fermentum Vel Mauris Consulting
                [phone] => 1-912-440-1465
                [type] => event
                [meeting_archive_id] => 69
                [date] => 02-08-2016
                [time] => 8.00
                [corp_aggregate_id] => 
                [inves_aggregate_id] => 
            )

        [3] => Array
            (
                [user_type] => 1
                [firstname] => Kenyon
                [lastname] => Martena
                [job_title] => 
                [email] => 
                [company_name] => Lorem Ipsum Sodales Incorporated
                [phone] => 1-446-161-3194
                [type] => event
                [meeting_archive_id] => 70
                [date] => 15-07-2016
                [time] => 8.00
                [corp_aggregate_id] => 
                [inves_aggregate_id] => 
            )

    )

I need to sort this array by date

Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

0

You can try this snippet as following:

foreach ($originalArray as $key => $part){ $sort[$key] = strtotime($part['date']); } array_multisort($sort, SORT_DESC, $originalArray);

Rashedul Islam Sagor
  • 1,943
  • 14
  • 20
-1

you can try with usort:

usort($array, function($a, $b) {
   $d1 = strtotime($a['date']);
   $d2 = strtotime($b['date']);
   return $d1 - $d2;
});

$a and $b are arrays which are part of main array $array and then do comparing their times.

Original definition of usort is:

bool usort ( array &$array , callable $value_compare_func )

and callable function is:

int callback ( mixed $a, mixed $b )

in out case, callable function is:

function($a, $b) {
   $d1 = strtotime($a['date']);
   $d2 = strtotime($b['date']);
   return $d1 - $d2;
}

usort works that take array as first argument and second argument callable function. That callable function except to get two element of the array. Internally it uses quicksort algorithm and callable function should return integer value 0, 1 or -1.

If the two dates(or anything what should be compared) are equal, the function should return 0.

If the first element is greater, it should return 1 If the second element is greater it should return -1

The input array (first parameter in usort function) is passed by reference, so you will have your array immediately sorted after calling usort.

Marko Krstic
  • 1,417
  • 1
  • 11
  • 13