I have an array of street names sorted alphabetically that I have gathered from a web service. This array exists on the server side.
On the client side, a user starts typing the name of the street he lives on and AJAX is used to return a list of the closest match to the partial street name, plus the next 9 street names in the array (the list is updated while he is typing).
For example, if the user typed "al", I would expect the results to be something like the following:
- Albany Hwy
- Albens Vale
- Alcaston Rd
- Alex Wood Dr
- Alice Rd
- Allawah Ct
- Allen Rd
- Alloway Pl
- Allwood Av
- Alola St
- Amanda Dr
This is my try at it:
$matches = array();
for($i = 0; $i < count($streetNames); $i++)
{
if( (stripos($streetNames, $input) === 0 && count($matches) == 0) || count($matches) < 10 ){
$matches[] = $streetNames[$i];
} else {
break;
}
}
Does anyone else know a faster way?
Please note: I have no control over how this list is obtained from the database - it's from an external web service.