检查PHP数组中是否有重复的元素(如果不为空) [英] Check for repeated elements in PHP array (if not empty)

查看:56
本文介绍了检查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(""))))  

这篇关于检查PHP数组中是否有重复的元素(如果不为空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆