This question already has an answer here:
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.
</div>
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