dongzheng7165 2013-06-17 22:33
浏览 27
已采纳

尝试在<select>中将数组的内容作为<options>进行回显

I have array that looks like this when I var_dump() it:

array(1) { ["bussiness"]=> array(5) { [0]=> string(8) "business" [1]=> string(9) "bossiness" [2]=> string(8) "busyness" [3]=> string(9) "bushiness" [4]=> string(9) "fussiness" } }

I am trying to get each of those elements to be an inside a single .

I thought it would be as simple as iterating through the array with a foreach and echoing each element in the array inbetween an and , but I am trying that and it doesn't seem to be working.

The output that I am getting is a with two s one says Array and the other is blank.

So the markup is like this:

<select>
    <option>Array</option>
    <option></option>
</select>

What am I doing wrong and why isn't the code echoing the array's contents. It is not even iterating through the entire array, just two items hence the two s.

Here is my code:

var_dump($bad_words);
echo "Did you mean: <select> 
";
foreach($bad_words as $suggestion){
    echo "<option>".$suggestion."</option> 
";
}
echo "</select>";
  • 写回答

3条回答 默认 最新

  • doudi8829 2013-06-17 22:35
    关注

    You are dealing with an array of arrays

    Your code should read.

    var_dump($bad_words);
    echo "Did you mean: <select> 
    ";
    foreach($bad_words["bussiness"] as $suggestion){
        echo "<option>".$suggestion."</option> 
    ";
    }
    echo "</select>";
    

    If you only ever want to deal with the first element in the nested array you can use

    var_dump($bad_words);
    $term = array_shift($bad_words);
    echo "Did you mean: <select> 
    ";
    foreach($term as $suggestion){
        echo "<option>".$suggestion."</option> 
    ";
    }
    echo "</select>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现