$states = array(
"NSW" => "NSW",
"VIC" => "VIC",
"QLD" => "QLD",
"SA" => "SA",
"WA" => "WA",
"TAS" => "TAS",
"NT" => "NT",
"ACT" => "ACT",
"OTH" => "OTH (AUS)",
"OVS" => "OVS (Overseas)"
);
现在我希望值为select
和变量的值。
foreach ($states as $key => $value) {
$form_ret .= "<option value='".$key."'>".$key[1]."</option>";
}
但我无法从OVS (Overseas)
获得OVS
的确切值。
我真正想要的是这样的:
<select name="State" id="State" >
<option value="NSW">NSW </option>
<option value="VIC">VIC </option>
<option value="QLD">QLD </option>
<option value="SA">SA </option>
<option value="WA">WA </option>
<option value="TAS">TAS </option>
<option value="NT">NT </option>
<option value="ACT">ACT </option>
<option value="OTH">OTH (AUS)</option>
<option value="OVS">OVS (Overseas)</option>
</select>
有没有人对我的案子有所了解?提前谢谢
答案 0 :(得分:1)
将$key[1]
替换为$value
:
foreach ($states as $key => $value) {
$form_ret .= "<option value='".$key."'>".$value."</option>";
}
答案 1 :(得分:0)
我认为您打算使用$value
代替$key[1]
foreach ($states as $key => $value) {
$form_ret .= "<option value='".$key."'>".$value."</option>";
}
答案 2 :(得分:0)
键[1]的值不正确。它表示键的1个字符串
必须使用$ value来显示正确的
答案 3 :(得分:0)
echo '<select name="State" id="State">';
foreach ($states as $key => $value){
echo '<option value="'.$key.'">'.$value.'</option>';
}echo '</select>';
这是你应该获得的HTML
<select id="State" name="State">
<option value="NSW">NSW</option>
<option value="VIC">VIC</option>
<option value="QLD">QLD</option>
<option value="SA">SA</option>
<option value="WA">WA</option>
<option value="TAS">TAS</option>
<option value="NT">NT</option>
<option value="ACT">ACT</option>
<option value="OTH">OTH (AUS)</option>
<option value="OVS">OVS (Overseas)</option>
</select>