PHP 重新排序月份名称数组 [英] PHP re-order array of month names

查看:41
本文介绍了PHP 重新排序月份名称数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列当前排序的月份名称,例如 ("April", "August", "February") 等.我想重新排序此列表,使其按正常的月份顺序排列,例如 ("January"", "二月", "三月")

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")

这个数组是从 SHOW TABLES 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.

推荐答案

将月份转换为数值.然后使用 sort()

Convert the months to a numerical value. Then order your array numerically using sort()

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

@Matthew 的回答似乎很有效:

@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆