2017-08-08 40 views
0

我有一個多維數組中的數據,它的格式爲'28 -Jul-17'。我想按升序或降序排列我的數據。我可以對我的多維數組進行排序,但我不知道如何對數據進行排序。我的問題是,我應該在這個函數中使用哪個標記來獲得我需要的結果?目前我正在使用SORT_DESC或SORT ASC,並且這不會按照時間順序對我的日期進行排序。使用array_multisort對'28 -jul-17'類型的日期進行排序?

我使用了以下內容:

array_multisort($fieldOfInterest, SORT_DESC , $arrayOfDictionary); 

排序類型標識:

SORT_REGULAR - compare items normally (don't change types) 
SORT_NUMERIC - compare items numerically 
SORT_STRING - compare items as strings 
SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale() 
SORT_NATURAL 
+0

你需要使用自定義排序函數,該函數 –

+0

默認標誌不會神奇地知道如何排序一些自定義日期格式。你必須自己正確地解析日期到一個可比較的值。 →https://stackoverflow.com/a/17364128/476 – deceze

+0

添加數組結構plz – Justinas

回答

1

你不能用array_multisort()默認標誌這樣做,因爲他們不知道定製日期格式

代替它,你可以這樣做如下: -

function compare_dates($a, $b) 
{ 
    $t1 = strtotime($a['date']); 
    $t2 = strtotime($b['date']); 
    return ($t1 >$t2) ? 1:-1; //changing 1 and -1 position will make it descending 
}  
usort($array, 'compare_dates'); // here $array is multi-dimensional array 

輸出: - https://eval.in/842881https://eval.in/842882

相關問題