如何在选择标记中仅显示数组的唯一值作为选项? [英] How to display only unique values of a array as option in a select tag?
本文介绍了如何在选择标记中仅显示数组的唯一值作为选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下数组:
$yrs = array(2015,2014,2015,2013,2015,2014);
我只需要在组合框中显示唯一值。
I need to display only the unique value inside a combo box.
我尝试过:
print_r(array_unique($yrs));
但是它显示如下:
Array ( [0] => 2015 [1] => 2014 [3] => 2013 )
组合框内只需要2015、2014、2013。我怎样才能只提取这些值?
I need to have only 2015, 2014, 2013 inside the combo box. How can i extract only these values?
我的代码:
<?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>
我需要分别使用不同的选项获得2015 2014 2013。
I need to get 2015 2014 2013 each in different options.
推荐答案
这回答了我的问题:
$yrss=array_unique($yrs);
foreach ($yrss as $key=>$value)
{
echo "<option>";
echo $value;
echo "</option>";
}
这篇关于如何在选择标记中仅显示数组的唯一值作为选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文