PHP重新排序月份名称的数组 [英] PHP re-order array of month names
问题描述
我目前正在有序如月名的阵列(四月,八五,二月)等。我想再次为了这个名单,使其处于正常秩序月如(一,月,月)
I have a array of month names currently ordered like ("April", "August", "February") etc. I would like to re order this list so that it is in a normal month order such as ("January", "February", "March")
这阵正从一个节目表的SQL查询填充,不幸的是SHOW TABLES没有一个ORDER BY参数,所以我想我最好的办法就是将它们添加到一个数组中,并重新排序阵列得到什么我要找
This array is getting populated from a SHOW TABLES sql query and unfortunately the SHOW TABLES does not have a ORDER BY parameter so I think my best bet is to add them to an array and reorder the array to get what I am looking for.
推荐答案
转换数月的数值。然后为了你的数字阵列使用排序()
Convert the months to a numerical value. Then order your array numerically using sort()
您可以使用这样一个问题:转换月份,从名称数量
You can use this question: convert month from name to number
@马修的回答似乎运作良好:
@Matthew's answer seems to work well:
$date = date_parse('July');;
echo $date["month"];
工作液
$months = array("April", "August", "February");
usort($months, "compare_months");
var_dump($months);
function compare_months($a, $b) {
$monthA = date_parse($a);
$monthB = date_parse($b);
return $monthA["month"] - $monthB["month"];
}
这篇关于PHP重新排序月份名称的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!