Get the keys for duplicate values in an 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) Result: $duplicates Array ( 1 => a ) // $result Array ( 2 => be 3 => c 4 => d ) // $unique_keys Array ( 0 => 2 1 => 3 2 => 4 ).

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.

A ) // $result Array ( 2 => be 3 => c 4 => d ) // $unique_keys Array ( 0 => 2 1 => 3 2 => 4 ).

This will return values, not keys. – cypher Jun 23 at 22:06 @cypher - You're right. I forgot to include array_keys.

I've updated my answer. – Francois Deschenes Jun 23 at 22:09 Also, interesting question would be if he wanted to print out all the duplicate keys - in your case 0 and 1 for a – cypher Jun 23 at 22:12 1 @cypher - A simple array_keys(array_intersect($array, $duplicates)) would do just that. :) – Francois Deschenes Jun 23 at 22:17 +1 Nice answer.

I like the unique / diff approach. But OP asked not for the unique, but those keys that are not unique. So probably you should not only put that into comment but into your answer as well.

– hakre Jun 23 at 22:33.

$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) }.

Function get_keys_for_duplicate_values($my_arr,$clean=false){ if($clean){ return array_unique($my_arr); } $dups=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); } } } 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.

1 you could shorten the code path when $clean === true by putting if ($clean === true) { return array_unique($my_arr); } at the top ;) – Dereleased Jun 23 at 22:25 true that thanks:) – Trey Jun 23 at 22:28 By the way, fastest method I've tested so far, handles 100 iterations of an 100,000+ element array in approx 0.04463 seconds per iteration! – Dereleased Jun 23 at 22:41 Changed my testing methodology a bit and while still fastest, actual iteration time is closer to 0.1811 seconds; the array had (on average) 165,000 elements, 100,000 of which were unique. – Dereleased Jun 23 at 23:02.

I really like Francois answer, here is something I came up with that preserves keys. I'll answer the first question first: $array = array('2011-06-21', '2011-06-22', '2011-06-22'); /** * flip an array like array_flip but * preserving multiple keys per an array value * * @param array $a * @return array */ function array_flip_multiple(array $a) { $result = array(); foreach($a as $k=>$v) $result$v=$k ; return $result; } $hash = array_flip_multiple($array); // filter $hash based on your specs (2 or more) $hash = array_filter($hash, function($items) {return count($items) > 1;}); // get all remaining keys $keys = array_reduce($hash, 'array_merge', array()); var_dump($array, $hash, $keys); output is: # original array array(3) { 0=> string(10) "2011-06-21" 1=> string(10) "2011-06-22" 2=> string(10) "2011-06-22" } # hash (filtered) array(1) { "2011-06-22"=> array(2) { 0=> int(1) 1=> int(2) } } # the keys array(2) { 0=> int(1) 1=> int(2) } So now the second question: Just use the $hash to obtain the keys for the value: var_dump($hash'2011-06-22'); returns the keys. Benefit is, if you need to check multiple values, data is already stored in the hash and available for use.

$v : 0; if (!array_key_exists($v, $seen)) { $seen$v = $k; continue; } if ($return_first &&! Array_key_exists($v, $dups)) { $dups$vk = $seen$v; } $dups$vk = $k; } return $return_by_key? $dups : $dups0; } If both optional parameters are true, it returns an array of arrays; the key of each child array will be the value which was not unique, and the values of the array will be all those keys which had that value.

If the first optional parameter is false, then only keys after the first instance of a non-unique value will be returned (i.e. , for the given array, each value returns only one key, the second time it occurred, instead of the first). If the second parameter is optional, then instead of returning an array of arrays, it returns a flat array containing all duplicate keys (exactly which keys it returns are dictated by the prior optional parameter).

Here's a dumpprint_r, cause it's prettier: print_r(getDupKeys($array)); Array ( 2011-06-22 => Array ( 0 => 1 1 => 2 ) 2011-06-23 => Array ( 0 => 3 1 => 4 ) 2011-06-239-218 => Array ( 0 => 5 1 => 6 ) 2011-06-227 => Array ( 0 => 7 1 => 8 ) 2011-06-229 => Array ( 0 => 9 1 => 10 ) 2011-06-27 => Array ( 0 => 11 1 => 12 ) 2011-06-29 => Array ( 0 => 14 1 => 15 ) 2011-06-30 => Array ( 0 => 16 1 => 17 ) 2011-07-01 => Array ( 0 => 18 1 => 19 ) 2011-06-23 => Array ( 0 => 20 1 => 21 ) 2011-06-23 => Array ( 0 => 22 1 => 23 ) 2011-06-23 => Array ( 0 => 24 1 => 25 ) 2011-06-229 => Array ( 0 => 26 1 => 27 ) 2011-06-237 => Array ( 0 => 28 1 => 29 ) 2011-06-235 => Array ( 0 => 30 1 => 31 ) ) print_r(getDupKeys($array, false)); Array ( 2011-06-22 => Array ( 0 => 2 ) 2011-06-23 => Array ( 0 => 4 ) 2011-06-239-218 => Array ( 0 => 6 ) 2011-06-227 => Array ( 0 => 8 ) 2011-06-229 => Array ( 0 => 10 ) 2011-06-27 => Array ( 0 => 12 ) 2011-06-29 => Array ( 0 => 15 ) 2011-06-30 => Array ( 0 => 17 ) 2011-07-01 => Array ( 0 => 19 ) 2011-06-23 => Array ( 0 => 21 ) 2011-06-23 => Array ( 0 => 23 ) 2011-06-23 => Array ( 0 => 25 ) 2011-06-229 => Array ( 0 => 27 ) 2011-06-237 => Array ( 0 => 29 ) 2011-06-235 => Array ( 0 => 31 ) ) print_r(getDupKeys($array, true, false)); Array ( 0 => 1 1 => 2 2 => 3 3 => 4 4 => 5 5 => 6 6 => 7 7 => 8 8 => 9 9 => 10 10 => 11 11 => 12 12 => 14 13 => 15 14 => 16 15 => 17 16 => 18 17 => 19 18 => 20 19 => 21 20 => 22 21 => 23 22 => 24 23 => 25 24 => 26 25 => 27 26 => 28 27 => 29 28 => 30 29 => 31 ) print_r(getDupKeys($array, false, false)); Array ( 0 => 2 1 => 4 2 => 6 3 => 8 4 => 10 5 => 12 6 => 15 7 => 17 8 => 19 9 => 21 10 => 23 11 => 25 12 => 27 13 => 29 14 => 31 ).

Here is a code dude $your_array = array(0 => '2011-06-21', 1 => '2011-06-21', 2 => '2011-06-21', 3 => '2011-06-21', 4 => '2011-06-21', 5 => '2011-06-217', 6 => '2011-06-217', 7 => '2011-06-218', 8 => '2011-06-218', 9 => '2011-06-217', 10 => '2011-06-217', 11 => '2011-06-217', 12 => '2011-06-217', 13 => '2011-06-217- 28', 14 => '2011-06-219', 15 => '2011-06-219', 16 => '2011-06-218', 17 => '2011-06-218', 18 => '2011-06-218', 19 => '2011-06-218', 20 => '2011-06-21', 21 => '2011-06-21', 22 => '2011-06-21', 23 => '2011-06-21', 24 => '2011-06-21', 25 => '2011-06-21', 26 => '2011-06-218', 27 => '2011- 11-06-217', 28 => '2011-06-219', 29 => '2011-06-219', 30 => '2011-06-213', 31 => '2011-06-213'); $keys_of_duplicated = array(); $array_keys = array(); foreach($your_array as $key => $value) { //- get the keys of the actual value $array_keys = array_keys($your_array, $value); //- if there is more then one key collected we register it if(count($array_keys) > 1) { //- foreach key that have the same value we check if i'ts already registered foreach($array_keys as $key_registered) { //- if not registered we register it if(!in_array($key_registered, $keys_of_duplicated)) { $keys_of_duplicated = $key_registered; } } } } var_dump($keys_of_duplicated); $keys_of_duplicated is now the array that contains the keys of duplicated arrays ;) bye.

1 In my testing, this times out (60 seconds) on a sufficiently large array (100,000 in my test). – Dereleased Jun 23 at 22:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions