我们如何使用php在数组中找到重复值? [英] How can we find the Duplicate values in array using php?

查看:93
本文介绍了我们如何使用php在数组中找到重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,我们如何检测数组中的重复条目...

I would like to know, how can we detect the duplicate entries in array...

类似

$array = array("192.168.1.1", "192.168.2.1","192.168.3.1","192.168.4.1","192.168.2.1","192.168.2.1","192.168.10.1","192.168.2.1","192.168.11.1","192.168.1.4") ;

我想获取数组中使用的Duplicity数量(C类唯一). 像这样

I want to get the number of Duplicity used in array (C class unique). like this

192.168.1.1 = unique
192.168.2.1 = Duplicate
192.168.3.1 = unique
192.168.4.1 = unique
192.168.2.1 = Duplicate
192.168.2.1 = Duplicate
192.168.10.1 = unique
192.168.2.1 = Duplicate
192.168.11.1 = unique
192.168.1.4 = Duplicate (Modified)

我尝试过这种样式的代码

I tried this code like this style

$array2 = array() ;

foreach($array as $list ){

$ips = $list;

$ip = explode(".",$ips);

$rawip = $ip[0].".".$ip[1].".".$ip[2] ;

array_push($array2,$rawip);

}

但是我无法以正确的方式设置数据,也无法进行循环以匹配数据.

but i am unable to set the data in right manner and also unable to make the loop for matching the data.

修改后的值

谢谢

SAM

推荐答案

尝试一下:这将为您提供每个值的计数

Try this : this will give you the count of each value

$array = array("192.168.1.1", "192.168.2.1","192.168.3.1","192.168.4.1","192.168.2.1","192.168.2.1","192.168.10.1","192.168.2.1","192.168.11.1") ;

$cnt_array = array_count_values($array)

echo "<pre>"; 
print_r($cnt_array);

$res = array();
foreach($cnt_array as $key=>$val){
   if($val == 1){
      $res[$key] = 'unique';
   }
   else{
      $res[$key] = 'duplicate';
   }
}

echo "<pre>";
print_r($res);

这篇关于我们如何使用php在数组中找到重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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