PHP重新排序月份名称数组

前端之家收集整理的这篇文章主要介绍了PHP重新排序月份名称数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一系列月份名称目前订购如(“四月”,“八月”,“二月”)等.我想重新订购此列表,以便它处于正常的月份顺序,如(“一月”,“二月三月”)

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

将月份转换为数值.然后使用sort()以数字顺序排列数组

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

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

  1. $date = date_parse('July');;
  2. echo $date["month"];

工作方案

  1. $months = array("April","August","February");
  2.  
  3. usort($months,"compare_months");
  4. var_dump($months);
  5.  
  6. function compare_months($a,$b) {
  7. $monthA = date_parse($a);
  8. $monthB = date_parse($b);
  9.  
  10. return $monthA["month"] - $monthB["month"];
  11. }

猜你在找的PHP相关文章