dtol41388 2011-07-22 10:40
浏览 51

如何使用日期值对数组进行排序

I generate an array with rearranged date values in it

$totert = array(
    array('2011','07','25'),
    array('2011','07','27'),
    array('2011','06','25'),
    array('2011','06','02'),
    array('2011','05','25'),
    array('2011','05','15')
);

how to arrange the value based on date i mean

  • 2011-05-15
  • 2011-05-25
  • 2011-06-02
  • 2011-06-25
  • 2011-07-25
  • 2011-07-27

could someone help me

  • 写回答

3条回答 默认 最新

  • douxiaochun4964 2011-07-22 10:42
    关注

    Use usort() and provide a custom comparison function.

    $totert = array(
        array('2011','07','25'), 
        array('2011','07','27'), 
        array('2011','06','25'), 
        array('2011','06','02'), 
        array('2011','05','25'), 
        array('2011','05','15')
    );
    usort($totert, function($a, $b) {
        return (int)implode('', $a) - (int)implode('', $b);
    });
    
    print_r(array_map(function($v) {
        return implode('-', $v);
    }, $totert));
    
    评论

报告相同问题?