How to sort a date array in PHP

二次信任 提交于 2019-11-26 04:46:28

问题


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


回答1:


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');



回答2:


Actually, use this:

usort($array, "cmp");

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

:)




回答3:


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.




回答4:


You could also use an anonymous function.

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


来源:https://stackoverflow.com/questions/597863/how-to-sort-a-date-array-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!