Sam
Sam

Reputation: 53

How can we find the Duplicate values in array using php?

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

Upvotes: 1

Views: 21036

Answers (6)

Bhupesh
Bhupesh

Reputation: 881

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;
?>

Upvotes: 0

user2918630
user2918630

Reputation: 1

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]; 

Upvotes: 0

Shelly Chen
Shelly Chen

Reputation: 15

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);
}

Upvotes: -1

Prasanth Bendra
Prasanth Bendra

Reputation: 32730

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

Upvotes: 4

Tapas Pal
Tapas Pal

Reputation: 7207

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

Upvotes: 0

Devang Rathod
Devang Rathod

Reputation: 6736

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)

Upvotes: 0

Related Questions