1

I need to compare the array values with each other.These values are unique IDs. So, have to check whether ID values are repeated.

<?php
 $id=array("firstid2012","secondid2014","thirddid2010","fourthid2014");
 $idcount=count($id);
 for($i=0;$i<$idcount;$i++){
 //how to compare?? 
 }  
 ?>

If repeated id is true, then i have to change the value of that array value.So I need to know which array value is repeated also.

6
  • no need of for loop. Try array_unique Commented Nov 19, 2014 at 12:09
  • Are you trying to remove duplicated values in the array, or display values that are repeated? Maybe you can give an example of the array values and what you expect? Commented Nov 19, 2014 at 12:12
  • I don't want to remove duplicated values. I want to display the repeated array value. Commented Nov 19, 2014 at 12:13
  • What's the value of $idvalues? Can you give an example? Commented Nov 19, 2014 at 12:18
  • Are you sure the first line is correct? Shouldn't it be $id = array("firstid2012","secondid2014","thirddid2010","fourthid2014"); Commented Nov 19, 2014 at 12:25

7 Answers 7

2
if (count($idvalues) == count(array_unique($idvalues))){
  //ALL VALUES ARE DISTINCTS
}
else {
  //THERE ARE DUPLICATED VALUES
  $duplicated=array();
  $visited=array();
  foreach($idvalues as $value){
      if (in_array($value,$visited)){
         $duplicated[]=$value;
      }
      $visited[]=$value;
  }
  $duplicated=array_uniq($duplicated);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This code is useful to check duplicated values. But how to get which array value is duplicated
Am getting a warning "in_array() expects parameter 2 to be array"
1

Some functions of interest to you:

array_unique: Remove duplicate values

http://php.net/manual/en/function.array-unique.php

array_intersect: Return values that occur in in more than one array.

http://php.net/manual/en/function.array-intersect.php

Comments

0

This is the fastest way to get all the unique values from an array:

$unique = array_keys(array_flip($array));

In the backend it uses a hashmap, whereas if you use array_unique that just iterates over an over the array, which is very inefficient. The difference is in orders of magnitude.

Comments

0

You could use array_unique() to get an array of all unique values and then compare the size against the original array:

if (count(array_unique($submitted_genres)) !== count($submitted_genres)) {
// there's at least one dupe
}

Comments

0

You no need to run any loop for that just use array_unique(); i added fourthid2014 twice

$id[] = array("firstid2012", "secondid2014", "thirddid2010", "fourthid2014", "fourthid2014");
print_r($id[0]); // print it 5 values 
$result = array_unique($id[0]);
print_r($result);// print it 4 values 

Comments

-1

You can use array_unique() function to remove duplicate values please refer this url for more info http://www.w3schools.com/php/func_array_unique.asp

Comments

-1

A simple way would be

<?php
 $id[]=$idvalues;
 $idcount=count($id);
 for($i=0;$i<$idcount;$i++){
   for($ii=0; $ii<$idcount;$ii++)
   {
    if( $i != $ii ) //We don't want to compare the same index to itself
    {
      if( $id[$i] == $id[$ii] )
      {
        //Found same values at both $i and $ii
        //As the code is here, each duplicate will be detected twice
      }
    }
 }  
?>

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.