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

时间:2022-07-15 20:45:44

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

4 个解决方案

#1


41  

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.

使用ISO(yyyy-mm-dd)格式而不是“英语”格式,然后使用kso​​rt函数以正确的顺序获取它们。

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.

没有必要删除连字符,ksort将对字符串键进行字母数字比较,并且yyyy-mm-dd格式可以很好地工作,因为词法顺序与实际日期顺序相同。

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:

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

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

uksort($array, 'cmp');

#2


10  

Actually, use this:

实际上,使用这个:

usort($array, "cmp");

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

:)

:)

#3


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.

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

#4


2  

You could also use anonymous function.

您也可以使用匿名函数。

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

#1


41  

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.

使用ISO(yyyy-mm-dd)格式而不是“英语”格式,然后使用kso​​rt函数以正确的顺序获取它们。

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.

没有必要删除连字符,ksort将对字符串键进行字母数字比较,并且yyyy-mm-dd格式可以很好地工作,因为词法顺序与实际日期顺序相同。

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:

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

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

uksort($array, 'cmp');

#2


10  

Actually, use this:

实际上,使用这个:

usort($array, "cmp");

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

:)

:)

#3


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.

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

#4


2  

You could also use anonymous function.

您也可以使用匿名函数。

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