PHP重新排序月份名称数组

Ron*_*Ron 8 php arrays

我有一系列月份名称目前订购如("四月","八月","二月")等.我想重新订购此列表,以便它处于正常的月份顺序,如("一月","二月三月")

这个数组是从SHOW TABLES sql查询中填充的,不幸的是SHOW TABLES没有ORDER BY参数,所以我认为最好的办法是将它们添加到数组中并重新排序数组以获得我想要的内容.

Tch*_*upi 17

将月份转换为数值.然后使用数字命令您的阵列sort()

您可以使用此问题:将月份从名称转换为数字

@Matthew的回答似乎运作良好:

$date = date_parse('July');;
echo $date["month"];
Run Code Online (Sandbox Code Playgroud)

工作方案

$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"];
}
Run Code Online (Sandbox Code Playgroud)