0

Please someone help me how to sort dates in array

i have array called $cont_history and it is a multi dimensional array, so now i need to sort this array by KEY VALUE (DATE)

   $cont_history = Array ( 
        [20-02-2015] => Array ( [activity] => 'GATE IN',  [by] => '',  [location] => 'COLOMBO, [depot] => 'CNLS' ) 
        [15-03-2015] => Array ( [activity] => 'GATE OUT', [by] => '',  [location] => 'COLOMBO, [depot] => 'CNLS' ) 
        [18-03-2015] => Array ( [activity] => 'GATE IN',  [by] => '',  [location] => 'CHENNAI, [depot] => 'GOBAL') 
        [05-04-2015] => Array ( [activity] => 'GATE OUT', [by] => '',  [location] => 'CHENNAI, [depot] => 'GOBAL') 
        [10-04-2015] => Array ( [activity] => 'GATE IN',  [by] => '',  [location] => 'MUMBAI,  [depot] => 'CONS' ) 
        [13-05-2015] => Array ( [activity] => 'GATE OUT', [by] => '',  [location] => 'MUMBAI,  [depot] => 'CONS' ) 
        [10-02-2015] => Array ( [activity] => 'Container Bought', [by] => 'CARU', [location] => 'COLOMBO', [depot] => 'CNLS' ) 
        [07-05-2015] => Array ( [activity] => 'Container Sold',   [by] => 'TCPL', [location] => 'MUMBAI',  [depot] => 'CONS' ) 
        )

after sorting the array this is what output i am expecting,

   $cont_history = Array ( 
        [20-02-2015] => Array ( [activity] => 'GATE IN',  [by] => '',  [location] => 'COLOMBO, [depot] => 'CNLS' ) 
        [15-03-2015] => Array ( [activity] => 'GATE OUT', [by] => '',  [location] => 'COLOMBO, [depot] => 'CNLS' ) 
        [18-03-2015] => Array ( [activity] => 'GATE IN',  [by] => '',  [location] => 'CHENNAI, [depot] => 'GOBAL') 
        [05-04-2015] => Array ( [activity] => 'GATE OUT', [by] => '',  [location] => 'CHENNAI, [depot] => 'GOBAL') 
        [10-04-2015] => Array ( [activity] => 'GATE IN',  [by] => '',  [location] => 'MUMBAI,  [depot] => 'CONS' ) 
        [07-05-2015] => Array ( [activity] => 'Container Sold',   [by] => 'TCPL', [location] => 'MUMBAI',  [depot] => 'CONS' ) 
        [13-05-2015] => Array ( [activity] => 'GATE OUT', [by] => '',  [location] => 'MUMBAI,  [depot] => 'CONS' ) 
        )

the date is DD-MM-YYYY format. I dont have any idea how to do this sorting. please someone help me how to do this. thank you.

Jonathan John
  • 165
  • 1
  • 3
  • 12
  • 1
    [uksort()](http://www.php.net/manual/en/function.uksort.php), with a callback that converts the dates to timestamps or DateTime objects and compares them – Mark Baker Jul 28 '15 at 13:38

1 Answers1

2

You can use "uksort()" to sort the array using a custom function:

function sortByDate($a, $b) {
   return strtotime($a) > strtotime($b);
}

$cont_history = Array ( 
    '20-02-2015' => Array ( 'activity' => 'GATE IN',  'by' => '',  'location' => 'COLOMBO', 'depot' => 'CNLS' ), 
    '15-03-2015' => Array ( 'activity' => 'GATE OUT', 'by' => '',  'location' => 'COLOMBO', 'depot' => 'CNLS' ), 
    '18-03-2015' => Array ( 'activity' => 'GATE IN',  'by' => '',  'location' => 'CHENNAI', 'depot' => 'GOBAL'), 
    '05-04-2015' => Array ( 'activity' => 'GATE OUT', 'by' => '',  'location' => 'CHENNAI', 'depot' => 'GOBAL'), 
    '10-04-2015' => Array ( 'activity' => 'GATE IN',  'by' => '',  'location' => 'MUMBAI',  'depot' => 'CONS' ), 
    '13-05-2015' => Array ( 'activity' => 'GATE OUT', 'by' => '',  'location' => 'MUMBAI',  'depot' => 'CONS' ), 
    '10-02-2015' => Array ( 'activity' => 'Container Bought', 'by' => 'CARU', 'location' => 'COLOMBO', 'depot' => 'CNLS' ),
    '07-05-2015' => Array ( 'activity' => 'Container Sold',   'by' => 'TCPL', 'location' => 'MUMBAI',  'depot' => 'CONS' ) 
);

uksort($cont_history, 'sortByDate');

print_r($cont_history);

Demo: http://3v4l.org/33RZg

t.h3ads
  • 1,833
  • 7
  • 15
  • Actually what is happening in this line ?? uksort($cont_history, 'sortByDate'); function sortByDate($a, $b) { return strtotime($a) > strtotime($b); } – Jonathan John Jul 28 '15 at 13:50
  • 1
    The array `$cont_history` is sorted using a custom function `sortByDate`. This function receives two key values to compare in `$a` and `$b`, e.g. `$a` is `20-02-2015` and `$b` is `15-03-2015`. I use `strtotime()` to convert the date strings into a timestamp because the dates can be compared better this way. – t.h3ads Jul 28 '15 at 13:52