2017-08-08 61 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

相关问题