2015-02-24 118 views
-1
$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和變量的值。PHP數組顯示值和變量

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> 

有沒有人有一個想法,我案件?在此先感謝

+2

'$ form_ret。=「<選項值='「。$ key。」'>「。$ value。」「;'也許使用值變量?! – Rizier123 2015-02-24 01:44:54

+0

而不是'$ key [1]'你只需使用'$ value'? – 2015-02-24 01:45:05

+1

Déjàvue? @ Rizier123 - *下方... * – 2015-02-24 01:51:01

回答

1

替代$key[1]$value

foreach ($states as $key => $value) { 
    $form_ret .= "<option value='".$key."'>".$value."</option>"; 
} 
0

我想你想用$value,而不是$key[1]

foreach ($states as $key => $value) { 
    $form_ret .= "<option value='".$key."'>".$value."</option>"; 
} 
0

鍵[1]是不是值正確。它代表字符在你的關鍵的串1

必須使用$值顯示右

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>