PHP Binary Search Example

There’s many ways to search an array in PHP.

Here’s 3 simple examples.

Note that most of the times it really doesn’t matter how you do the search there’s always situations where you can’t simply use array_search function for example. This is especially true when it’s an associative array or you’re dealing with a bit more exmplex data.

At the end of the way use whichever approach suits your needs. Always sticking to the most simple approach.

The binary search below is a special type of search. It starts from the middle. Then it checks whether the item we’re searching is on the left or right hand side.. if it’ left, it searches left diving array into 2 each time. Same for right.

You can test it here:

https://search.verax5.repl.co

Leave a comment

Your email address will not be published. Required fields are marked *