gpt4 book ai didi

PHP重新排序月份名称数组

转载 作者:可可西里 更新时间:2023-11-01 12:27:11 24 4
gpt4 key购买 nike

我有一个当前排序的月份名称数组,例如(“April”、“August”、“February”)等。我想重新排序此列表,以便它处于正常的月份顺序,例如(“January” ", "二月", "三月")

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

最佳答案

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

对数组进行数字排序

您可以使用这个问题:convert month from name to number

@Matthew 的回答似乎很有效:

$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重新排序月份名称数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12424968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com