Get the keys for duplicate values in an array

I have the following array:

$myarray = Array("2011-06-21", "2011-06-22", "2011-06-22", "2011-06-23", "2011-06-23", "2011-06-24", "2011-06-24", "2011-06-25", "2011-06-25", "2011-06-26");
var_dump($myarray);

Result:

Array (
    [0] => 2011-06-21
    [1] => 2011-06-22
    [2] => 2011-06-22
    [3] => 2011-06-23
    [4] => 2011-06-23
    [5] => 2011-06-24
    [6] => 2011-06-24
    [7] => 2011-06-25
    [8] => 2011-06-25
    [9] => 2011-06-26
)
  1. Now how can I display the keys with duplicate values? Here the function should NOT return ([0],[9]) since there are no duplicates with the values.
  2. How to find the keys for the same value, eg. for "2011-06-25" it should return [7],[8]

function get_keys_for_duplicate_values($my_arr, $clean = false) {
    if ($clean) {
        return array_unique($my_arr);
    }

    $dups = $new_arr = array();
    foreach ($my_arr as $key => $val) {
      if (!isset($new_arr[$val])) {
         $new_arr[$val] = $key;
      } else {
        if (isset($dups[$val])) {
           $dups[$val][] = $key;
        } else {
           $dups[$val] = array($key);
           // Comment out the previous line, and uncomment the following line to
           // include the initial key in the dups array.
           // $dups[$val] = array($new_arr[$val], $key);
        }
      }
    }
    return $dups;
}

obviously the function name is a bit long;)

Now $dups will contain a multidimensional array keyed by the duplicate value, containing each key that was a duplicate, and if you send "true" as your second argument it will return the original array without the duplicate values.

Alternately you could pass the original array as a reference and it would adjust it accordingly while returning your duplicate array


I'll answer the second question first. You want to use array_keys with the "search_value" specified.

$keys = array_keys($array, "2011-06-29")

In the example below, $duplicates will contain the duplication values while $result will contain ones that are not duplicates. To get the keys, simply use array_keys.

<?php

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

// Unique values
$unique = array_unique($array);

// Duplicates
$duplicates = array_diff_assoc($array, $unique);

// Unique values
$result = array_diff($unique, $duplicates);

// Get the unique keys
$unique_keys = array_keys($result);

// Get duplicate keys
$duplicate_keys = array_keys(array_intersect($array, $duplicates));

Result:

// $duplicates
Array
(
    [1] => a
)

// $result
Array
(
    [2] => b
    [3] => c
    [4] => d
)

// $unique_keys
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
)

// $duplicate_keys
Array
(
    [0] => 0
    [1] => 1
)

I had a similar problem as question #1 from the OP. All I needed were the keys for duplicate values in my original array. Here's what I came up with:

$array = array('yellow', 'red', 'green', 'brown', 'red', 'brown');

$counts = array_count_values($array);
$filtered = array_filter($counts, function($value) {
    return $value != 1;
});
$result = array_keys(array_intersect($array, array_keys($filtered)));

And for the output:

print_r($result);
Array
(
    [0] => 1
    [1] => 3
    [2] => 4
    [3] => 5
)

$array = array(0 => "1", 1 => "1", 2 => "2", 3 => "3");
$count = array();
foreach($array as $key => $value) {
  if(!isset($count[$value])) {
    $count[$value] = 0;
  }
  $count[$value]++;
}


$result = array_filter($count, function($value) {
  return $value > 1;
});

$result = array_keys($result);

var_dump($result);

Output

array(1) {
  [0]=>
  int(1)
}