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 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法