1

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

Something like

$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") ;

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.

modified values

Thanks

SAM

Sam
  • 53
  • 1
  • 2
  • 9
  • possible duplicate of [How to detect duplicate values in PHP array?](http://stackoverflow.com/questions/1170807/how-to-detect-duplicate-values-in-php-array) – Dipesh Parmar Mar 04 '13 at 10:33

6 Answers6

4

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);
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
0

use array_unique($array) function. it will give you below output.

Array
(
    [0] => 192.168.1.1
    [1] => 192.168.2.1
    [2] => 192.168.3.1
    [3] => 192.168.4.1
    [6] => 192.168.10.1
    [8] => 192.168.11.1
)

And total duplicate count must be :

array_count_values($array)
Devang Rathod
  • 6,650
  • 2
  • 23
  • 32
0

Try this, hope it'll work

$FinalArray=array();
$arrayLen=count($array);
for($i=0; $i<$arrayLen;  $i++)
{
    if(!in_array($array[$i],$FinalArray))
        $FinalArray[]=$array[$i];
}

Now in $FinalArray you got all the unique ip

Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
0

Try this:

for ($i = 0; $i < count($array); $i++) 
    for ($j = $i + 1; $j < count($array); $j++)
        if ($array[$i] == $array[$j])
            echo  $array[$i]; 
fantaghirocco
  • 4,761
  • 6
  • 38
  • 48
0

use in_array() function to check value is or not in array

<?php
$output ='';
$array = array(0, 1, 1, 2, 2, 3, 3);
$isArraycheckedvalue = array();
for ($i=0; $i < sizeof($array); $i++) 
{
    $eachArrayValue = $array[$i];
    if(! in_array($eachArrayValue, $isArraycheckedvalue)) 
    {
        $isArraycheckedvalue[] = $eachArrayValue;
        $output .= $eachArrayValue. " Repated no <br/>";      
    }
    else
    {
        $isArraycheckedvalue[] = $eachArrayValue;
        $output .= $eachArrayValue. " Repated  yes <br/>";
    }
}
echo $output;
?>
Bhupesh
  • 883
  • 7
  • 16
-1

find the Duplicate values in array using php

function array_repeat($arr){
    if(!is_array($arr))
        return $arr;
    $arr1 = array_unique($arr);
    $arr3 = array_diff_key($arr,$arr1);
    return array_unique($arr3);
}
HiDeoo
  • 10,353
  • 8
  • 47
  • 47