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

1

6 Answers 6

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Please see the updated question. i said the duplicate c' class
You forgot to put semicolon in the end $cnt_array = array_count_values($array);
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)

1 Comment

Please see the updated question. i said the duplicate c' class
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

1 Comment

Please see the updated question. i said the duplicate c' class
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]; 

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.