Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用switch语句将月份分配给数组键值_Php_Arrays - Fatal编程技术网

Php 使用switch语句将月份分配给数组键值

Php 使用switch语句将月份分配给数组键值,php,arrays,Php,Arrays,我需要打印相应数组值(1=jan,2=feb…等)的月份名称,后跟每月费用。我已经取得了相当大的进展,可以打印“月[1]$2997.10”,例如,但不知道如何以“一月$2997.10”的格式打印。我知道我错过了一些简单的东西,但我已经尝试了我能想到的一切,只收到了错误消息。提前感谢你的帮助 $monthly_expense = array( '1' => 2997.10, '2' => 921.00,

我需要打印相应数组值(1=jan,2=feb…等)的月份名称,后跟每月费用。我已经取得了相当大的进展,可以打印“月[1]$2997.10”,例如,但不知道如何以“一月$2997.10”的格式打印。我知道我错过了一些简单的东西,但我已经尝试了我能想到的一切,只收到了错误消息。提前感谢你的帮助

     $monthly_expense = array(    '1' => 2997.10,
                              '2' => 921.00,
                              '3' => 371.99,
                              '4' => 1928.00,
                              '5' => 1206.00,
                              '6' => 10190.33,
                              '7' => 8390.35,
                              '8' => 3009.93,
                              '9' => 4803.30,
                              '10'=> 1212.30,
                              '11'=> 225.90,
                              '12'=> 594.65
                              );

 //Your program starts here!
switch ($monthly_expense) {
case 1:
    $month = 'Jan';
    break;
case 2:
    $month = 'Feb';
    break;

case 3:
    $month = 'Mar';
    break;
case 4:
    $month = 'Apr';
    break;
case 5:
    $month = 'May';
    break;
case 6:
    $month = 'Jun';
    break;
case 7:
    $month = 'Jul';
    break;
case 8:
    $month = 'Aug';
    break;
case 9:
    $month = 'Sep';
    break;
case 10:
    $month = 'Oct';
    break;
case 11:
    $month = 'Nov';
    break;
case 12:
    $month = 'Dec';
    break;
default:
    $month = 'Not a valid month!';

    break;
}

for ($count = 1; $count < sizeof($monthly_expense)+1; $count++)
     printf ("Month [%d]: $%.2f\n", $monthly_expense[$count]);


 //Compute the total of all salaries
 $totalExpense = 0.0;

 foreach ($monthly_expense as $value)
     $totalExpense += $value;

 printf ("The total company expenses for the year is $%.2f.\n", $totalExpense);
$monthly\u expense=数组('1'=>2997.10,
'2' => 921.00,
'3' => 371.99,
'4' => 1928.00,
'5' => 1206.00,
'6' => 10190.33,
'7' => 8390.35,
'8' => 3009.93,
'9' => 4803.30,
'10'=> 1212.30,
'11'=> 225.90,
'12'=> 594.65
);
//你的节目从这里开始!
交换机(每月费用){
案例1:
$month='Jan';
打破
案例2:
$month='Feb';
打破
案例3:
$month='Mar';
打破
案例4:
$month='Apr';
打破
案例5:
$month='May';
打破
案例6:
$month='Jun';
打破
案例7:
$month='Jul';
打破
案例8:
$month='Aug';
打破
案例9:
$month='Sep';
打破
案例10:
$month='Oct';
打破
案例11:
$month='Nov';
打破
案例12:
$month='Dec';
打破
违约:
$month='不是有效月份!';
打破
}
对于($count=1;$count
使用每月包含以下内容的数组更有效:

$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.', 13=>'Total');

for ($count = 1; $count < sizeof($monthly_expense)+1; $count++)
    printf("%s $%.2f <br>", $months[$count], $monthly_expense[$count]);
$months=array(1=>‘一月’、2=>‘二月’、3=>‘三月’、4=>‘四月’、5=>‘五月’、6=>‘六月’、7=>‘七月’、8=>‘八月’、9=>‘九月’、10=>‘十月’、11=>‘十一月’、12=>‘十二月’、13=>‘总计’;
对于($count=1;$count”,$months[$count],$monthly\u expense[$count]);

您可以为月份名称添加另一个数组(因为
开关
语句看起来很糟糕):

然后:

for ($i = 1; $i <= count($monthly_expense); $i++) {
   printf ("Month [%s]: $%.2f\n", $months[$i], $monthly_expense[$i]);
}

对于($i=1;$i您无论如何都不需要月数组,只需使用和从
$monthly\u expense
数组中的键获取月:

foreach( $monthly_expense as $month => $value ) {
    printf( "Month [%s]: $%.2f\n", date("M", mktime(0, 0, 0, $month, 1) ), $value );
}

救了我键入数组…gracias amoeba
foreach( $monthly_expense as $month => $value ) {
    printf( "Month [%s]: $%.2f\n", date("M", mktime(0, 0, 0, $month, 1) ), $value );
}