0

Which function should be used, array_unique or array_search, while comparing speed.

I have an array that is full of duplicate entries, now I have 2 options to make my array unique. I am confused, which function should I use for better performance?

4
  • 1
    If you want to get only unique items, array_search won't help you. So, your question is meaningless. Commented Aug 5, 2013 at 11:08
  • I hope he means 'array_filter' Commented Aug 5, 2013 at 11:09
  • No, I mean exactly array_search. I will explain how.... Add the first element to a new array. Take the second element in aaray and search it in the new array with array_search, if it exists, continue else add it to the new array and so on.. Commented Aug 5, 2013 at 11:17
  • What is the subject of the entries? depending on what the entries contain, one solution may be better than another. Commented Aug 5, 2013 at 11:27

1 Answer 1

1

Personally I would use array_unique(), since it does exactly what you want. This native method is developed for this exact goal, you can assume it will have the best performance.

Native functions are usually faster then any function you can create yourself. And even if it's not, future updates of the language (php) will be able to improve performance.

That being said: array_search is quite heavy, so depending on the size of your array it might be slow as hell to use in this case.

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

1 Comment

I find your answer reasonable :)

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.