按字段end_date ASC排序多维数组

时间:2013-12-13 09:55:47

标签: php arrays sorting date

我正在尝试按字段end_date ASC

对数组进行排序

这是我的数组(包含更多数据,有时是相同的日期)

0 (Array, 5 elements)
    type (String, 5 characters ) short
    description (String, 8 characters ) textext
    enjoyed (Boolean) FALSE
    start_date (String, 24 characters ) 2013-12-03T01:00:00.000Z
    end_date (String, 24 characters ) 2014-01-26T01:00:00.000Z

1 (Array, 5 elements)
    type (String, 5 characters ) short
    description (String, 4 characters ) text
    enjoyed (Boolean) FALSE
    start_date (String, 24 characters ) 2013-12-19T01:00:00.000Z
    end_date (String, 24 characters ) 2014-06-25T02:00:00.000Z

2 (Array, 5 elements)
    type (String, 5 characters ) short
    description (String, 17 characters ) black en Platinum
    enjoyed (Boolean) FALSE
    start_date (String, 24 characters ) 2013-12-13T01:00:00.000Z
    end_date (String, 24 characters ) 2014-01-31T01:00:00.000Z

这是我的尝试:

function cmp($a, $b)
{
    global $array;
    return strcmp($array[$a]['end_date'], $array[$b]['end_date']);
}

uksort($array, 'cmp');

但结果是

1 
1
1
1

哪里出错了?在dateformat?

有人能指出我正确的方向或提供帮助吗?

2 个答案:

答案 0 :(得分:1)

我使用这个函数来按照他的一个列对每个多维数组进行排序:

function sortArrayBy($array , $column_name,$sort=SORT_DESC){
  foreach ($array as $key => $row) {
   $column[$key]  = $row[$column_name];
  }
  array_multisort($column, $sort, $array);
  return $array;
 }

这样称呼:

<?php sortArrayBy($yourArray,'end_date',SORT_ASC) ; ?>

答案 1 :(得分:0)

试试这个,

<?php
function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

$a = array(3, 2, 5, 6, 1);

usort($a, "cmp");

foreach ($a as $key => $value) {
    echo "$key: $value\n";
}
?>

参考:http://www.php.net/manual/en/function.usort.php