Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: What is the searching algorithm implemented in array_search()?

I have an array of items on which I would like to apply search function. I am considering sorting the array and simply applying binary search for now as it need not be too complex, however if I run into problems I'll try other methods.

My question is; what is the search algorithm used in the array_search()? If it is indeed binary search I can use that.

like image 232
bonl Avatar asked May 05 '26 18:05

bonl


2 Answers

It has to be sequential search, because the array might not be sorted.

If you need to search an array often, use array_flip to convert it to an associative array where the values become the keys. Looking up keys in an array is a hash lookup.

like image 66
Barmar Avatar answered May 08 '26 08:05

Barmar


PHP performs a linear search - here's the source

like image 38
Paul Dixon Avatar answered May 08 '26 08:05

Paul Dixon