这是我的数组值
$months = array(01 => "Jan", 02 => "Feb", 03 => "Mar", 04 => "Apr", 05 => "May", 06 => "Jun", 07 => "Jul", 08 => "Aug", 09 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec");
我的输出就像这样
Array ( [1] => Jan [2] => Feb [3] => Mar [4] => Apr [5] => May [6] => Jun [7] => Jul [0] => Sep [10] => Oct [11] => Nov [12] => Dec )
为什么?
答案 0 :(得分:1)
如果你想要前导零,你必须使用字符串键:
$months = array("01" => "Jan", "02" => "Feb", "03" => "Mar", "04" => "Apr", "05" => "May", "06" => "Jun", "07" => "Jul", "08" => "Aug", "09" => "Sep", "10" => "Oct", "11" => "Nov", "12" => "Dec");
显示当前月份:
echo($months[date('m')]);
答案 1 :(得分:1)
您必须删除数组索引中的前导零。
整数值中的前导零以octal
编号格式定义数字。
如果您使用09
,这意味着您要在八进制编号系统中使用9
,但该系统仅支持0
- 7
,以便PHP将转换它到0
。
请将您的代码更改为:
$months = array(1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr", 5 => "May", 6 => "Jun", 7 => "Jul", 8 => "Aug", 9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec");
答案 2 :(得分:0)
You can try it.
$months = array("01" => "Jan", "02" => "Feb", "03" => "Mar", "04" => "Apr", "05" => "May", "06" => "Jun", "07" => "Jul", "08" => "Aug", "09" => "Sep", "10" => "Oct", "11" => "Nov", "12" => "Dec");
$now = new \DateTime('now');
$month = $now->format('m');
echo $months[$month];