Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top