检查PHP数组中是否有重复的元素(如果不为空)
问题描述:
我说我有这个数组:
$check_post = array(
$_POST["a_post"],
$_POST["b_post"],
$_POST["c_post"],
$_POST["d_post"],
$_POST["e_post"],
$_POST["f_post"],
$_POST["g_post"],
$_POST["h_post"],
$_POST["i_post"]
);
我想检查是否重复了该数组的任何元素,所以我得到的最好是:
I want to check whether any elements of this array are repeated, so the best I got is this:
if (count(array_unique($check_post)) < count($check_post))
echo "Duplicate";
else
echo "NO Duplicate";
除了将一个文本区域留空(允许)之外,其他条件都可以,所以这很好.
Which works fine except for the fact that if more that one textarea is left blank (which is allowed) it gives me FALSE.
我想要的是不考虑(count(array_unique())
顺便说一句,我尝试使用 empty()
和 array_values($ check_post)
,但我无法解决它.
BTW I have tried with empty()
and with array_values($check_post)
but I cant get around it.
提前谢谢!!请要求任何必要的澄清.
Thanks in advance!! please ask for any needed clarification.
答
要从比较中删除所有空值,可以添加 array_diff()
:
To remove all the empty values from the comparison you can add array_diff()
:
if (count(array_unique(array_diff($check_post,array("")))) < count(array_diff($check_post,array(""))))