将短月份名称与具有完整月份名称的数组中的值匹配

时间:2015-10-29 22:44:41

标签: php arrays

我在我的数据中获得了短月份名称,例如1月,2月,3月,我需要将其转换为完整的月份名称,例如1月,2月,3月。

$content .= '<h3>'.$m[1].''; // gives me say 'Jan' for instance

我想我会从构建一个月的数组开始 -

$fullmonths = array("January","February",etc)

然后,将$ m [1]缩写月份名称与fullmonths数组进行比较并在找到的匹配项中提取完整月份名称的最佳方法是什么。感谢您提供任何帮助&#39;

1 个答案:

答案 0 :(得分:1)

未测试代码,但理论上应该有效。基本上PHP中的数组是实现中的哈希表。因此,您可以将短名称指定为键,将值指定为月份的全名。

现在您可以搜索密钥:

$fullMonthNames = array();
$fullMonthNames['Jan']='January';
$fullMonthNames['Feb']='February';
$fullMonthNames['Mar']='March';
echo 'Full name of this month is: '.$fullMonthNames[$m[1]]."\n";