如何按键'日期'对数组进行排序?

时间:2013-01-23 10:24:40

标签: php arrays sorting

  

可能重复:
  how to sort a multidemensional array by an inner key
  How to sort a multidimensional array by a certain key?

我希望我的数组按日期asc排序,怎么办?

<?php
    //how to resort a array by key 'date'?
    $arr=array(
        array(
            'name'=>'a',
            'date'=>'2012-10-21 18:00:00'
        ),
        array(
            'name'=>'x',
            'date'=>'2013-10-21 18:00:00'
        ),
        array(
            'name'=>'d',
            'date'=>'2011-10-21 18:00:00'
        )
    );
?>

1 个答案:

答案 0 :(得分:3)

function sort_by_date($a, $b) {
  return( $a['date'] < $b['date'] ? -1 : ($a['date'] > $b['date']) ? 1 : 0 );
}

usort($arr, 'sort_by_date');