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!

1

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.