0

I have a PHP Array, and I want to extract from it the duplicated values and keep the corresponding keys for each value, in a new Array.

This is my starting Array

Array
(
    [0] => ABC123-A
    [1] => BG231-B
    [2] => ABC123-A
    [3] => BG231-E
    [4] => BG231-F
    [5] => BG231-G
    [6] => BG231-H
    [7] => ABC123-A
    [8] => BG231-J
    [9] => BG231-K
    [10] => ABC123-A
)

And this is what I want to have:

Array
(
    [0] => ABC123-A
    [2] => ABC123-A
    [7] => ABC123-A
    [10] => ABC123-A
)

How can I do that ?

Thanks for help.

3
  • 1
    What if there are more than one duplicated value? Commented May 3, 2012 at 13:32
  • I have more than one duplicated value, I want to keep them (with the corresponding key), because I need these values... Commented May 3, 2012 at 13:36
  • This answer get's you all duplicated values - every duplicate value has it's own array and is keyed by the value. Each array contains the original keys. Commented May 3, 2012 at 13:40

5 Answers 5

1

Test this out:

$return = array_unique($array);
var_dump(
    array_diff($array, array_diff($return, array_diff_assoc($array, $return)))
);
Sign up to request clarification or add additional context in comments.

Comments

1

PHP has no function build in that does this, but you can combine array_count_values and array_intersect to obtain the array(s) you want.

The first one is used to know which values do exist in your array (and counts them so it's easy to filter out every non-duplicate one), and the second function does preserve the keys. If you know the duplicated value upfront, you only need to use the latter:

$duplicatesArray = array_intersect($array, array($duplicate));

Full example:

/**
 * @param array $array
 * @param int $threshold (optional) minimum number of elements per group
 * @return array
 */
function array_group_by_value(array $array, $threshold = 1)
{
    $grouped = array();
    foreach(array_count_values($array) as $value => $count)
    {
        if ($count < $threshold) continue;
        $grouped[$value] = array_intersect($array, array($value));
    }
    return $grouped;
}

Usage Example:

$test = array('a', 'b', 'a', 'a', 'b', 'c');

var_dump(array_group_by_value($test, 2));

Output:

array(2) {
  ["a"]=>
  array(3) {
    [0]=>
    string(1) "a"
    [2]=>
    string(1) "a"
    [3]=>
    string(1) "a"
  }
  ["b"]=>
  array(2) {
    [1]=>
    string(1) "b"
    [4]=>
    string(1) "b"
  }
}

Comments

1

If you need all duplicates in one array, You can try:

$unique = array_unique($array); //get unique values
$diff = array_diff_assoc($array, $unique); //get duplicates
$intersect = array_intersect($unique, $diff); //get unique values with duplicates
return $intersect+$diff; //join unique with duplicates and duplicates

Comments

0
$duplicated=null;

foreach($yourarray as $k => $v){
    foreach($yourarray as $k2 => $v2){
        if($v == $v2 && $k2 != $k){
            $duplicated[$k]=$v;
        }
    }
}

1 Comment

I don't think I recommend this answer (versus other answers) because it is always making n x n conditional checks with the nested loop. I mean, the same outcome is achieved with fewer iterations by implementing a break. 3v4l.org/Doo0R I'll admit that I haven't thought too deeply about this task, but array_count_values() feels like a direct way of identifying duplicates without re-searching the input array over and over.
-1

You can use array_unique().http://php.net/manual/en/function.array-unique.php

1 Comment

Judging by the question, this is the opposite. OP is asking to KEEP the duplicates, not remove them

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.