按日期按日期降序对数组进行排序 [英] Sort array by date in descending order by date in php

查看:348
本文介绍了按日期按日期降序对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,我想按日期对它进行排序.我无法按日期将其正确降序排序.请帮忙.

I have an array and I want to sort it by date. I am not able to sort it properly by date in descending order. Please help.

Array
(
[1] => Array
    (
        [1] => 11/05/2013
        [2] => Executive Planning Day
    )

[2] => Array
    (
        [1] => 13/06/2013
        [2] => Middle Leaders Planning Day
    )

[3] => Array
    (
        [1] => 12/07/2013
        [2] => New Staff Induction Day
    )

[4] => Array
    (
        [1] => 13/04/2013
        [2] => Staff Conference Day No. 1
    )

[5] => Array
    (
        [1] => 14/04/2013
        [2] => Staff Conference Day No. 2
    )

[6] => Array
    (
        [1] => 15/02/2013
        [2] => Staff Conference Day No. 3
    )

[7] => Array
    (
        [1] => 16/03/2013
        [2] => Australia Day
    )
)

推荐答案

尝试这样

function sortFunction( $a, $b ) {
    return strtotime($a[1]) - strtotime($b[1]);
}
usort($data, "sortFunction");   //Here You can use asort($data,"sortFunction")

或者您可以像(只是建议)一样尝试细节

or you may try by detail like(its just suggestion)

function sortFunction($a,$b)
    if ($a[1] == $b[1]) return 0;
    return strtotime($a[1]) - strtotime($b[1]);
}
usort($data,"sortFunction");

由于 strtotime不遵循d/m/Y格式,请尝试这样

$orderByDate = $my2 = array();
foreach($data as $key=>$row)
{
    $my2 = explode('/',$row[1]);
    $my_date2 = $my2[1].'/'.$my2[0].'/'.$my2[2];        
    $orderByDate[$key] = strtotime($my_date2);  
}    
array_multisort($orderByDate, SORT_DESC, $data);

这篇关于按日期按日期降序对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆