在 PHP 中,我有 2 个数组
$ex1 = array(a,b,c,d,e,f,g,h);
$ex2 = array(c,e,f);
这里如何将其与 PHP 页面中的多个选择选项集成
这里 ex1 是多选数组,例如
<select multiple name=slt[]>
</select>
ex2 值是所选的列表选项
最佳答案
类似于:
<?php
$ex1 = array('a','b','c','d','e','f','g','h');
$ex2 = array('c','e','f');
echo "<select multiple name=slt[]>";
foreach($ex1 as $val){
//in_array() checks if value from 1st array ($val) is present
//anywhere in the second array ($ex2)
//if yes, that option will be selected. I'm using ternary operator
//here instead of if statement
$selected = (in_array($val,$ex2))?' selected':'';
echo "<option value='".$val."'$selected>".$val."</option>";
}
echo "</select>";
?>
<强> PHP Demo
<强> Output Fiddle
关于php - php中二维数组的多重选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22906210/