20

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

3
  • 2
    Can you please use var_dump() on your data instead of making up your own format? At least when your own format is as ambiguous and misleading as what you posted here. ;-) Commented Feb 28, 2009 at 11:05
  • This question is unclear about the input structure -- making it less valuable as a dupe destination. Commented May 23, 2024 at 2:44
  • See also: Sort a 2d array by second level keys formatted as d M Y Commented Nov 2, 2024 at 16:53

4 Answers 4

43

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');
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. What function would I use if it were a (non-nested) normal indexed array rather than an associative array? ksort seems to only work with associative arrays AFAIK
10

Actually, use this:

usort($array, "cmp");

function cmp($a, $b){ 
    return strcmp($b['db'], $a['db']); 
}

:)

Comments

4
function cmp($a, $b) {    
    global $array;    
    return strcmp($array[$a]['db'], $array[$b]['db']); 
}    
uksort($array, 'cmp');

I think there is better to use usort() function instead of uksort(), because sometimes you can`t use global variables at all and anyway using global variables is not a good practice either.

Comments

2

You could also use an anonymous function.

// Sort in chronological order.
usort($array, function($a, $b) {
  return strcmp($a['db'], $b['db']);
});

2 Comments

This answer is fundamentally the same as older answers
It's using anonymous function where the others don't.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.