PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
function sort_history($a, $b) {
return strnatcmp($a['date_history'], $b['date_history']);
}
usort($history_items, 'sort_history');
However it shows the oldest dates first the and newest dates last. I need to reverse that so it shows the newest dates first and the oldest dates last.
$history_items = array();
$history_items[0]['id'] = "1";
$history_items[0]['date_history'] = "2007-08-20 13:04:57";
$history_items[0]['name'] = "test1";
$history_items[0]['details'] = "this is a test";
$history_items[1]['id'] = "2";
$history_items[1]['date_history'] = "2007-08-18 09:23:01";
$history_items[1]['name'] = "test2";
$history_items[1]['details'] = "this is a test as well";
$history_items[2]['id'] = "3";
$history_items[2]['date_history'] = "2007-08-21 12:11:01";
$history_items[2]['name'] = "test3";
$history_items[2]['details'] = "this is a test even more than the first two";
etc.. etc...
Basically I want the array to sort by the date_history. Newest to oldest. That code I first gave you almost does the trick however it shows it oldest to newest.
Your array is multidimensional so the sort and natsort functions are not going to behave the way you want. My php skills are a bit rusty, but I managed to put together this function:
I wrote this in a hurry this morning without doing any research, so if anyone has a better solution please post it here. I bet it can be optimized further. This is how you would use it on your array:
I really don't know what I was thinking. Read the whole post, make sure you understand the problem then reply. Got it!
Listen to arborint and ignore my post.
function rsort_history($a, $b) {
return strnatcmp($b['date_history'], $a['date_history']);
}
usort($history_items, 'rsort_history');
Mate, how could I be so blind... Yes, arborint's code did work. I failed to notice you switched the b and the a around. I thought you gave me exactly the same code but just added the letter r to the start of the functions.