如何在<select>中为Selected属性回显关联数组中的特定值

时间:2017-03-19 14:44:56

标签: php html css

任何人都可以帮我解决这个问题 $ person = array('abb'=&gt;'Abbie,'abi'=&gt;'阿比盖尔',ade'=&gt;'阿德莱德'); $ selection =''; foreach($ selection as $ abbreviation =&gt; $ name){     $ selection = $ selection。“&lt; option value = $ abbreviation selected =(我如何选择在这里违反默认选项)&gt; $ name&lt; / option&gt;”; }

1 个答案:

答案 0 :(得分:1)

试试这个

    $person = array('abb' => 'Abbie', 'abi' => 'Abigail', 'ade' => 'Adelaide');
$selection = '<select>';
foreach ($person as $abbreviation => $name) {
    $selection = $selection.'<option value="'.$abbreviation.'"';
    if($name=='Abigail')
        $selection .= 'selected';
    $selection .=  ">$name</option>";
}
$selection .= '</select>';
echo $selection;

如果我理解正确,这就是你想要的