PHP:检查如果一个数组的重复 [英] php: check if an array has duplicates

查看:122
本文介绍了PHP:检查如果一个数组的重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,这是一个非常明显的问题,那有这么正是这样做的一个功能,但我似乎无法找到它。在PHP中,我想知道如果我的数组中有重复的,尽可能有效的。我不想删除它们像 array_unique 确实,我并不特别想运行 array_unique 和比较它原来的数组,看看它们是相同的,因为这似乎是非常低效的。至于表现而言,预期条件是该阵列具有没有重复

I'm sure this is an extremely obvious question, and that there's a function that does exactly this, but I can't seem to find it. In PHP, I'd like to know if my array has duplicates in it, as efficiently as possible. I don't want to remove them like array_unique does, and I don't particularly want to run array_unique and compare it to the original array to see if they're the same, as this seems very inefficient. As far as performance is concerned, the "expected condition" is that the array has no duplicates.

我只是想能够做到像

if (no_dupes($array))
    // this deals with arrays without duplicates
else
    // this deals with arrays with duplicates

有我没有想到的任何明显的作用?结果
<一href=\"http://stackoverflow.com/questions/1170807/how-to-detect-duplicate-values-in-php-array\">http://stackoverflow.com/questions/1170807/how-to-detect-duplicate-values-in-php-array

是一种非常类似的问题,但是如果你读的问题,他要寻找array_count_values​​。

Is there any obvious function I'm not thinking of?
http://stackoverflow.com/questions/1170807/how-to-detect-duplicate-values-in-php-array
is a very similar question, however if you read the question, he's looking for array_count_values.

推荐答案

您可以这样做:

function has_dupes($array){
 $dupe_array = array();
 foreach($array as $val){
  if(++$dupe_array[$val] > 1){
   return true;
  }
 }
 return false;
}

这篇关于PHP:检查如果一个数组的重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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