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?

Guman Thakur
  • 27
  • 1
  • 4
  • 1
    If you want to get only unique items, array_search won't help you. So, your question is meaningless. – user4035 Aug 05 '13 at 11:08
  • I hope he means 'array_filter' – Amit Kriplani Aug 05 '13 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.. – Guman Thakur Aug 05 '13 at 11:17
  • What is the subject of the entries? depending on what the entries contain, one solution may be better than another. – Flosculus Aug 05 '13 at 11:27

1 Answers1

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.

Damien Overeem
  • 4,487
  • 4
  • 36
  • 55