I am wrote an array for all months in a year as follows:
$MNTH["01"] = "January";
$MNTH["02"] = "February";
$MNTH["03"] = "March";
$MNTH["04"] = "April";
$MNTH["05"] = "May";
$MNTH["06"] = "June";
$MNTH["07"] = "July";
$MNTH["08"] = "August";
$MNTH["09"] = "September";
$MNTH["10"] = "October";
$MNTH["11"] = "November";
$MNTH["12"] = "December";
When I do a variable dump on the keys of $MNTH with var_dump(array_keys($MNTH)),
I get:
array(12) {
[0]=> string(2) "01"
[1]=> string(2) "02"
[2]=> string(2) "03"
[3]=> string(2) "04"
[4]=> string(2) "05"
[5]=> string(2) "06"
[6]=> string(2) "07"
[7]=> string(2) "08"
[8]=> string(2) "09"
[9]=> int(10)
[10]=> int(11)
[11]=> int(12) }
I was expecting strings for the last three keys. How did it become integer? What should I do to correct this phenomenon?
PHP converts numeric keys to integers at the time you create the array element. It isn’t
array_keysthat is doing it. But there is a hack to get string numeric keys:Output:
But you won’t be able to access that key by subscript, so it’s not very useful.
If you must have string keys, you’ll need to prefix them with another non-numeric (or zero) character:
From the documentation: