2016-09-09 82 views
-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"); 

我的輸出這樣的

Array ([1] => Jan [2] => Feb [3] => Mar [4] => Apr [5] => May [6] => Jun [7] => Jul [0] => Sep [10] => Oct [11] => Nov [12] => Dec) 

爲什麼?

+0

奧格斯特不顯示在輸出數組 –

+0

你錯過了一個一個月 – Karthi

+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')]); 
+0

這是正在工作的.......謝謝 –

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

也請看看:int variable with leading zero?

+0

這是正確的答案,因爲它解釋了'爲什麼'。 –

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];