在下拉列表中显示关联数组的键名

时间:2017-06-01 09:04:47

标签: php

<?php
    $month=array("January"=>"01","February"=>"02","March"=>"03","April"=>"04",
                 "May"=>"05","June"=>"06","July"=>"07","August"=>"08","September"=>"09",
                "October"=>"10","November"=>"11","December"=>"12");


    echo"<select name='month'>";
    echo"<option>-select-</option>";
    for($i=0;$i<12;$i++)
    {

        echo "<option>".$month[$i]."</option>"."<br />";
    }
    echo"</select>";

      ?>

我想在下拉选项中显示名称1月,2月...并且还希望将其值放在另一个变量中。

4 个答案:

答案 0 :(得分:0)

$month=array("January"=>"01","February"=>"02","March"=>"03","April"=>"04",
                 "May"=>"05","June"=>"06","July"=>"07","August"=>"08","September"=>"09",
                "October"=>"10","November"=>"11","December"=>"12");

echo"<select name='month'>";
echo"<option>-select-</option>";

foreach($month as $key=>$value){

  echo "<option value='". $value ."'>".$key."</option>"."<br />";
}


 echo"</select>";

$ value将给出其值。

答案 1 :(得分:0)

在这种情况下你应该使用foreach

<?php
$month=array("January"=>"01","February"=>"02","March"=>"03","April"=>"04","May"=>"05","June"=>"06","July"=>"07","August"=>"08","September"=>"09","October"=>"10","November"=>"11","December"=>"12");
?>
<select name="month">
<?php foreach($month as $k => $v) {
    echo "<option value='" . $v . "'>" . $k . "</option>";
} ?>
</select>

我们将关联数组的键和值存储在单独的变量中并相应地回显它们。

答案 2 :(得分:0)

你可以使用foreach循环来完成它。

foreach ($month as $name => $value) {
        echo "<option>".$name."</option>"."<br />";
}

答案 3 :(得分:0)

使用foreach并打印键值

   <?php

    $month=array("January"=>"01","February"=>"02","March"=>"03","April"=>"04",
                     "May"=>"05","June"=>"06","July"=>"07","August"=>"08","September"=>"09",
                    "October"=>"10","November"=>"11","December"=>"12");

                    foreach($month as $key=>$value){
                      echo "<option>".$key."</option>"."<br />";
                    }
     ?>