Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数组中填充下拉列表_Php_Arrays_Drop Down Menu - Fatal编程技术网

Php 从数组中填充下拉列表

Php 从数组中填充下拉列表,php,arrays,drop-down-menu,Php,Arrays,Drop Down Menu,我有以下代码 $all = $ent->getAll($fed_code); Array ( [NC] => Array ( [101] => banana, [102] => orange, [103] => apple, ) ) $select = $ent->getSelected($fed_code); Array ( [101] => banana, ) 我想要的是,如果在$all数组中找

我有以下代码

$all  = $ent->getAll($fed_code);

 Array ( [NC] => Array (
    [101] => banana,
    [102] => orange,
    [103] => apple,
    )
    )


$select  = $ent->getSelected($fed_code);

Array
(
[101] => banana,
)
我想要的是,如果在$all数组中找到$select值,则用所选值填充下拉列表。 这是我目前掌握的密码

<?php
    foreach ($all as $orgKey => $list) { 
?>
<tr><td width="5%">
        <h5>Extra Fruits</h5>
    </td>
    <td width="10%">
        <label class="control-label">Fruits</label>
        <select class="input-xlarge" id="input" name="ent[]">   
            <option value="">Select</option>';

            <?   foreach ($list as $key => $value) { 
                    $selected = in_array($select, $key)?'selected="selected"':'';
                    echo $selected;
                ?>

                <option <?=$selected?> value="<?=$key?>"><?=$value?></option>
                <? } ?>
        </select>
    </td>
                        </tr>

额外的水果
水果
选择';
但是,似乎有什么不对劲,它没有选择任何东西。
有人有一些想法吗?

打印
$selected
会产生什么?另外,要从select标记中选择一个选项,语法不是如下所示:

<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>

沃尔沃汽车
萨博
大众汽车
奥迪

你真的需要里面的foreach吗? 我尝试了这个,它已经可以将您的选择与$select中的选择一起打印为select

<select class="input-xlarge" id="input" name="ent[]">   
    <? foreach ($all as $orgKey => $list) {
        $selected = in_array($select, $list) ? 'selected="selected"' : '';
    ?>
       <option <?=$selected?> value="<?=$orgKey?>"><?=$list?></option>
    <? } ?>
</select>

我刚刚意识到我已经交换了in_数组的参数。这应该可以做到。我还在选择标签中添加了多个选项,因此如果我们有多个选择,它将显示出来

<select multiple class="input-xlarge" id="input" name="ent[]">   
    <? foreach ($all as $orgKey => $list) {
        $selected = in_array($list, $select) ? 'selected="selected"' : '';
    ?>
       <option <?=$selected?> value="<?=$orgKey?>"><?=$list?></option>
    <? } ?>
</select>


如果您不想使用多个选项,则最后一个测试为选中的选项将是html中显示为选中的选项。

但是$list将回显数组,是吗?不是数组的内容?那你怎么解决呢?我的错,我已经交换了in_数组函数的参数$列表将只包含值,而不包含数组。请看我的更新。不是真的它不工作,我必须使它在不同的过程中与长代码和一点技巧。但现在没事了。无论如何,谢谢你。我只是对你的努力表示赞赏。