0

I checked other similar questions like php check array value for duplicate (which only gives a true or false result)

and How to detect duplicate values in PHP array? (which gets what values were duplicated, but not their keys it seems)

I have a need to look through the array and determine any duplicates for repeating to the end-user, including the array keys, so that I can echo those keys as "excel line numbers"

Sample Excel Spreadsheet:

E123456
E234567
E345678
E123456
E456789
E111111
E123456
E234567
E333333
E444444

Desired Result for end user:

E123456 was a duplicate, found on lines 1, 4, and 7
E234567 was a duplicate, found on lines 2 and 8

So I need to store both what value got duplicated AND the keys it was duplicated on.

I realize that this would be some combination of array_unique, array_diff, array_keys (and maybe a couple others), but I'm not sure what order to "stack" the calls in to get the desired result, without ending up with some brute-force method that causes the system to choke (the file size is potentially hundreds of lines)

TheMouseMaster
  • 298
  • 1
  • 10
  • You will not choke a system with a few hundred items. Have you looked at this: https://www.accountingweb.com/technology/excel/identifying-duplicate-values-in-an-excel-list – Eriks Klotins Feb 12 '19 at 17:08
  • iterate over your rows and fillan array where the key is the value you want to have unique and the value a list of the lines where it occurs. – Franz Gleichmann Feb 12 '19 at 17:08
  • @Eriks Klotins thanks, but I know it can be done in excel; the problem is, I have no control over whether the person who uploads that file bothers to do that beforehand :( – TheMouseMaster Feb 12 '19 at 18:27

1 Answers1

1

You can use array_count_values function to get amount of each value in the source array and then save only items that occurred more than once. Now array_keys with a 2nd argument returns keys of the array with that value

// find duplicates. They will be keys of the array
$res = array_count_values($arr);
$res = array_filter($res, function($x) { return $x > 1;});
// find indexes corresponding that duplicates values
foreach($res as $k=>&$v) {
   $v = array_keys($arr, $k, true);    
}
print_r($res);

demo

splash58
  • 26,043
  • 3
  • 22
  • 34