如何在 PHP 中对日期数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/597863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:12:38  来源:igfitidea点击:

How to sort a date array in PHP

phparraysarraylistassociative-arraysorting

提问by Click Upvote

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:

最初以 MySQL 日期格式获取电子邮件,因此我可以在这种状态下获取数组:

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.

也许当它采用这种格式时,我可以遍历它们,删除所有'-'(连字符)标记,以便将它们保留为整数,使用array_sort()它们对它们进行排序并再次遍历它们以对它们进行排序?如果有另一种方式,我会更喜欢,因为我会对每个用户进行 3 次循环。

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?

但是使用它,是否有任何方法可以仅根据 'db' 元素对数组进行排序?

Edit 2: Updated initial var_dump

编辑 2:更新初始 var_dump

回答by Alnitak

Use the ISO (yyyy-mm-dd) format rather than the "english" format, and then just use the ksortfunction to get them in the right order.

使用 ISO ( yyyy-mm-dd) 格式而不是“英文”格式,然后只需使用该ksort函数将它们按正确的顺序排列即可。

There's no need to remove the hyphens, ksortwill do an alphanumeric comparison on the string keys, and the yyyy-mm-ddformat works perfectly well as the lexical order is the same as the actual date order.

不需要删除连字符,ksort将对字符串键进行字母数字比较,并且yyyy-mm-dd格式非常有效,因为词法顺序与实际日期顺序相同。

EDITI 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 uksortas 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:

编辑我看到你现在已经更正了你的问题,以表明你实际上有一个数组数组,并且排序键在子数组中。在这种情况下,您应该uksort按照其他地方的建议使用,但我建议您根据 DB 格式的日期进行自己的编辑和排序,而不是解析人类可读的格式:

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

uksort($array, 'cmp');

回答by trend

Actually, use this:

实际上,使用这个:

usort($array, "cmp");

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

:)

:)

回答by Alexander

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.

我认为最好使用usort()function 而不是uksort(),因为有时您根本无法使用全局变量,无论如何使用全局变量也不是一个好习惯。

回答by Rafal Enden

You could also use an anonymous function.

您也可以使用匿名函数。

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