2013-12-13 72 views
0

我想排序字段END_DATE ASC通過現場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?

有人能指引我進入正確的方向還是提供幫助?

+0

W您是否在需要這些值時使用'uksort'?檢查:['usort'](http://php.net/usort) – AmazingDreams

回答

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) ; ?> 
+0

非常感謝這個片段,它確實工作:)我將繼續使用它。 –

+0

請標記爲已解決,以幫助其他人:) –

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