1

I have an array with values $somearray = array('car','bike','legs,'car'). I'd like to find out which of these values in $somearray are repeated and pick out the index. In this example the answer would be 'car' and the index of the array would be 0 and 3.

I'm wondering if this can be done simply in a few lines, perhaps using some PHP function that I don't know, or do I need to explicitly make comparison in nested loops?

TIA!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
laketuna
  • 3,832
  • 14
  • 59
  • 104

2 Answers2

3

The solution is pretty simple and I bet you can write it yourself. All you need is just 2 functions: array_count_values() and array_keys() with specified second argument (thanks to @prodigitalson)

zerkms
  • 249,484
  • 69
  • 436
  • 539
0

There's a good answer on this similar question - How to detect duplicate values in PHP array?

To get the array indexes, I would then filter any array value > 1 and get the indexes

Community
  • 1
  • 1
steve
  • 576
  • 1
  • 5
  • 12