duancai7002 2015-02-17 06:55
浏览 126
已采纳

如何在select标签中仅显示数组的唯一值作为选项?

Consider this array:

$yrs = array(2015,2014,2015,2013,2015,2014);

I need to display only the unique value inside a combo box.

I tried:

print_r(array_unique($yrs));

But it displays it like this:

Array ( [0] => 2015 [1] => 2014 [3] => 2013 )

I need to have only 2015, 2014, 2013 inside the combo box. How can i extract only these values?

My code:

<?php
    $yrs = array(2015,2014,2015,2013,2015,2014);
?>

<select name="yrs">
<?php

    echo "<option>";
    echo implode(",",array_unique($yrs));  //here it displays in same option 
    echo "</option>";

?>
</select>

I need to get 2015 2014 2013 each in different options.

  • 写回答

1条回答 默认 最新

  • doumo6356 2015-02-17 18:57
    关注

    This answers my question:

    $yrss=array_unique($yrs);
    foreach ($yrss as $key=>$value) 
    { 
    echo "<option>"; 
    echo $value; 
    echo "</option>"; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置