如何在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.
考虑这个数组: p>
$ yrs = array(2015) ,我需要在组合框中只显示唯一值。 p>
我试过了: p>
print_r(array_unique($ yrs));
code> pre>
但它显示了它 像这样: p>
Array([0] =&gt; 2015 [1] =&gt; 2014 [3] =&gt; 2013)
code> pre >
我需要在组合框中只有2015,2014,2013。 我怎样才能只提取这些值? p>
我的代码: p>
&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;
code> pre>
I 需要以不同的方式获得2015 2014 2013. p>
div>
答
This answers my question:
$yrss=array_unique($yrs);
foreach ($yrss as $key=>$value)
{
echo "<option>";
echo $value;
echo "</option>";
}