开发者

Sorting Date and Time in an Array in PHP

开发者 https://www.devze.com 2023-04-04 23:52 出处:网络
I need to sort this Array in ascending order using Date & Time , The Array i want to sort is listed below:-

I need to sort this Array in ascending order using Date & Time , The Array i want to sort is listed below:-

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] => 开发者_JAVA技巧OUT 
    [3] => GATE1 
    [4] => NULL 
) 


Use usort:

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

Or with PHP < 5.3:

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

Try it here: http://codepad.org/J8U2G32A

Alternatively, with array_multisort:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号