按日期按日期在php中按日期排序数组

前端之家收集整理的这篇文章主要介绍了按日期按日期在php中按日期排序数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个数组,我想按日期排序.我无法按日期降序排序.请帮忙.
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")

或者你可以尝试细节(它的建议)

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);
原文链接:https://www.f2er.com/php/133636.html

猜你在找的PHP相关文章