How can i get only duplicate values from an array in php?

You will need to make your function case insensitive to get the "Hello" => "hello" result you are looking for, try this method:

$arr = array(1=>'1233',2=>'12334',3 =>'Hello' ,4=>'hello', 5=>'U');

// Convert every value to uppercase, and remove duplicate values
$withoutDuplicates = array_unique(array_map("strtoupper", $arr));

// The difference in the original array, and the $withoutDuplicates array
// will be the duplicate values
$duplicates = array_diff($arr, $withoutDuplicates);
print_r($duplicates);

Output is:

Array
(
[3] => Hello
[4] => hello
)

Edit by @AlixAxel:

This answer is very misleading. It only works in this specific condition. This counter-example:

$arr = array(1=>'1233',2=>'12334',3 =>'Hello' ,4=>'HELLO', 5=>'U');

Fails miserably. Also, this is not the way to keep duplicates:

array_diff($arr, array_unique($arr));

Since one of the duplicated values will be in array_unique, and then chopped off by array_diff.

Edit by @RyanDay:

So look at @Srikanth's or @Bucabay's answer, which work for all cases (look for case insensitive in Bucabay's), not just the test data specified in the question.

How can i get only duplicate values from an array in php?

10 Years Ago

Show only duplicate values from array without built in function

$arr = array(3,5,2,5,3,9);
I want to show only common elements i.e 3,5 as output.

Edited 10 Years Ago by rajendra87

How about something outside of the box:

$array = array(3, 5, 2, 5, 3, 9);
$duplicates = array_duplicates($array);

function array_duplicates(array $array)
{
    return array_diff_assoc($array, array_unique($array));
}

Jump to Post

Thanks for necroposting this dead thread back to the current list - not.

Jump to Post

All 7 Replies

How can i get only duplicate values from an array in php?

10 Years Ago

The logic here is simple:

I am not a php developer so i am using c++ to explain this method !

int Array[6]= {3,5,2,5,3,9};

//create a for-loop for ArrayA

    for(int i=0; i<6; i++)
    {

//create a for-loop for checking element from ArrayA is equal to any other element


        for(int s=0; s<6; s++)
        {



// to check if terms are equal (note i!=s)

            if(Array[i]==Array[s] && i!=s)
            {

             //Print Array elements or store it in another array and the use any unique array function to see unique elements !
            }
        }
    }

Try to make the code for php yourself !

How can i get only duplicate values from an array in php?

10 Years Ago

I saw this same question here ! if you did not post it then here is the code:

$arr = array(3,5,2,5,3,9);
foreach($arr as $key => $val){
  //remove the item from the array in order 
  //to prevent printing duplicates twice
  unset($arr[$key]); 
  //now if another copy of this key still exists in the array 
  //print it since it's a dup
  if (in_array($val,$arr)){
    echo $val . " ";
  }
}

How can i get only duplicate values from an array in php?

10 Years Ago

that question was posted by me @rainbowMatrix

How can i get only duplicate values from an array in php?

blocblue 238 Posting Pro in Training Featured Poster

10 Years Ago

How about something outside of the box:

$array = array(3, 5, 2, 5, 3, 9);
$duplicates = array_duplicates($array);

function array_duplicates(array $array)
{
    return array_diff_assoc($array, array_unique($array));
}

How can i get only duplicate values from an array in php?

phorce 131 Posting Whiz in Training Featured Poster

10 Years Ago

Finally got there:

 0)
    {
        echo $val2 . ', ';
    }
}

?>

I doubt it will work for words, or characters, for this you will have to manipulate the array_keys.

Output:

3, 5,

Edited 10 Years Ago by phorce

How can i get only duplicate values from an array in php?

renuv 0 Newbie Poster

6 Years Ago

function hasDuplicate($array) {
            $defarray = array();
        $filterarray = array();
        foreach($array as $val){
            if (isset($defarray[$val])) {
                $filterarray[] = $val;
            }
            $defarray[$val] = $val;
        }
        return $filterarray;
}

How can i get only duplicate values from an array in php?

diafol

6 Years Ago

Thanks for necroposting this dead thread back to the current list - not.

Reply to this topic

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.

How can I get repeat value from an array in PHP?

Return Value: The array_unique() function returns the filtered array after removing all duplicates from the array. print_r( array_unique ( $a ));

How do you find duplicate values in an array?

function checkIfArrayIsUnique(myArray) { for (var i = 0; i < myArray. length; i++) { for (var j = 0; j < myArray. length; j++) { if (i != j) { if (myArray[i] == myArray[j]) { return true; // means there are duplicate values } } } } return false; // means there are no duplicate values. }

How do you find the duplicate number on a given integer array in PHP?

This is the only answer that returns only the duplicate values. – Redzarf. ... .
Extended to only give list of duplicate values (what I needed): return array_values(array_unique(array_diff_key($a, array_unique($a)))); – Autumn Leonard. ... .
Stop scrolling. Although it doesn't seem, this is the answer you are looking for..

How can I get unique values from two arrays in PHP?

The array_diff() (manual) function can be used to find the difference between two arrays: $array1 = array(10, 20, 40, 80); $array2 = array(10, 20, 100, 200); $diff = array_diff($array1, $array2); // $diff = array(40, 80, 100, 200);