I have an array in this format:
Array
(
[0] => Array
(
[28th February, 2009] => 'bla'
)
[1] => Array
(
[19th March, 2009] => 'bla'
)
[2] => Array
(
[5th April, 2009] => 'bla'
)
[3] => Array
(
[19th April, 2009] => 'bla'
)
[4] => Array
(
[2nd May, 2009] => 'bla'
)
)
I want to sort them out in the ascending order of the dates (based on the month, day, and year). What's the best way to do that?
Originally the emails are being fetched in the MySQL date format, so its possible for me to get the array in this state:
Array
[
['2008-02-28']='some text',
['2008-03-06']='some text'
]
Perhaps when its in this format, I can loop through them, remove all the '-'
(hyphen) marks so they are left as integars, sort them using array_sort()
and loop through them yet again to sort them? Would prefer if there was another way as I'd be doing 3 loops with this per user.
Thanks.
Edit: I could also do this:
$array[$index]=array('human'=>'28 Feb, 2009',
'db'=>'20080228',
'description'=>'Some text here');
But using this, would there be any way to sort the array based on the 'db' element alone?
Edit 2: Updated initial var_dump
Use the ISO (yyyy-mm-dd
) format rather than the "english" format, and then just use the ksort
function to get them in the right order.
There's no need to remove the hyphens, ksort
will do an alphanumeric comparison on the string keys, and the yyyy-mm-dd
format works perfectly well as the lexical order is the same as the actual date order.
EDIT I see you've now corrected your question to show that you've actually got an array of arrays, and that the sort key is in the sub-arrays. In this case, you should use uksort
as recommended elsewhere, but I would recommend that you go with your own edit and sort based on the DB formatted date, rather than by parsing the human readable format:
function cmp($a, $b)
{
global $array;
return strcmp($array[$a]['db'], $array[$b]['db']);
}
uksort($array, 'cmp');