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

如何在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.

图片转代码服务由CSDN问答提供 功能建议

考虑这个数组:

  $ yrs = array(2015)  ,我需要在组合框中只显示唯一值。 
 
  

我试过了:

  print_r(array_unique($ yrs)); 
   
 
 

但它显示了它 像这样:

  Array([0] =&gt; 2015 [1] =&gt; 2014 [3] =&gt; 2013)
   
 
 

我需要在组合框中只有2015,2014,2013。 我怎样才能只提取这些值?

我的代码:

 &lt;?php 
 $ yrs = array(2015,2014,2015,2013,2015,2014  ); 
?&gt; 
 
&lt; select name =“yrs”&gt; 
&lt;?php 
 
 echo“&lt; option&gt;”; 
 echo implode(“,”,array_unique($ yrs)  ));  //这里显示相同的选项
 echo“&lt; / option&gt;”; 
 
?&gt; 
&lt; / select&gt; 
   
 
 

I 需要以不同的方式获得2015 2014 2013.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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>"; 
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题