在PHP中对数组中的日期和时间进行排序

时间:2011-09-14 10:54:02

标签: php arrays

我需要使用Date& amp;按升序对此数组进行排序。时间,我要排序的数组如下: -

Array ( 
    [0] => 005
    [1] => 2011-09-12 12:28:46 
    [2] => IN 
    [3] => GATE1 
    [4] => NULL 
) 

Array (
     [0] => 003 
     [1] => 2011-09-12 12:23:43 
     [2] => IN 
     [3] => GATE1
     [4] => NULL 
)

Array (
    [0] => 001
    [1] => 2011-09-12 12:23:37
    [2] => IN
    [3] => GATE1
    [4] => NULL 
) 

Array ( 
    [0] => 002 
    [1] => 2011-09-12 12:23:34
    [2] => IN 
    [3] => GATE1 
    [4] => NULL 
)

Array ( 
    [0] => 012 
    [1] => 2011-09-12 12:23:29
    [2] => OUT 
    [3] => GATE1
    [4] => NULL 
)

Array ( 
    [0] => 010 
    [1] => 2011-09-12 12:23:25 
    [2] => OUT 
    [3] => GATE1 
    [4] => NULL 
) 

1 个答案:

答案 0 :(得分:4)

使用usort

usort($array, function($a, $b) {
    return strcmp($a[1], $b[1]);
});

或者使用PHP< 5.3:

function sort_cb($a, $b) {
    return strcmp($a[1], $b[1]);
}
usort($array, 'sort_cb');

在此处试试:http://codepad.org/J8U2G32A

或者,array_multisort

$dates = array_map(function($a) { return $a[1]; }, $array);
array_multisort($array, $dates);