我有一个数组,其中包含一年中带有switch语句和while循环的月份,但它不会显示数组的第一个元素,即1月。有人可以帮我解决这个问题吗?
$calendar = array (January, February, March, April, May, June, July, August, September, October, November, December);
while($e < sizeof($calendar)) {
$k++;
$days = 30;
$days2 = 31;
switch ($calendar[$e]) {
case $calendar[0]:
echo "Month ",$k,": $calendar[$e] has $days days <br>";
break;
case $calendar[1]:
echo "Month $k: $calendar[$e] has 28 or 29 days <br>";
break;
case $calendar[2]:
echo "Month $k: $calendar[$e] has $days2 days <br>";
break;
case $calendar[3]:
echo "Month $k: $calendar[$e] has $days days <br>";
break;
case $calendar[4]:
echo "Month $k: $calendar[$e] has $days days <br>";
break;
case $calendar[5]:
echo "Month $k: $calendar[$e] has $days days <br>";
break;
case $calendar[6]:
echo "Month $k: $calendar[$e] has $days2 days <br>";
break;
case $calendar[7]:
echo "Month $k: $calendar[$e] has $days days <br>";
break;
case $calendar[8]:
echo "Month $k: $calendar[$e] has $days days <br>";
break;
case $calendar[9]:
echo "Month $k: $calendar[$e] has $days2 days <br>";
break;
case $calendar[10]:
echo "Month $k: $calendar[$e] has $days days <br>";
break;
case $calendar[11]:
echo "Month $k: $calendar[$e] has $days2 days <br>";
break;
}
$e++;
}
答案 0 :(得分:1)
如评论中所述,您在使用之前未定义$e
。正如您所看到的,它会产生不可预测的结果。设置它按预期工作。
答案 1 :(得分:0)
如果您不介意,请使用日历功能cal_days_in_month()
:
$year = 2013
$arr_month_name = cal_info(0)['months'];
for ($month=1; $month < 12; $month+=1) {
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
echo "Month $month: $arr_month_name[$month] has $days days <br/>";
}