We built a new PHP API for ZIP code search. It's currently in the experimental stage, but so far in our tests it's been working well. I don't 100% like the interface, but at least now we'll have a starting point to refine from.
The new function is vae_zip_distance($from, $to). $from is the starting ZIP code and $to is the destination ZIP code. It returns a distance in miles. If $to is an array of ZIP codes, the function will return an associative array mapping the destination ZIP code to the distance.
If $to is a Vae object, the function will pull the ZIP code for each object by looking in the "zip" structure under that object. You may change the name of the structure we look for by passing it in as a third parameter. This mode will still return an associative array mapping the destination ZIP code to the distance, however it will also add an attribute "distance" to each Vae object passed in. This enables you to then filter by that.
Here's an example of showing stores in the Stores collection that are within 50 miles of the ZIP code passed in via the request parameter $zip:$stores = vae("stores");
$distances = vae_zip_distance($_REQUEST['zip'], $stores);
foreach ($stores as $store) {
if ($store->distance < 50) {
echo $store->name . " (" . number_format($store->distance, 2) . "miles away)\n";
}
}
If you wanted to do some sorting, you could use the PHP usort() function. This example would show the stores sorted from closest to farthest:function sort_by_distance($a, $b) {
return ($a->distance > $b->distance);
}
$stores = vae("stores");
$distances = vae_zip_distance($_REQUEST['zip'], $stores);
$stores = $stores->toArray();
usort($stores, "sort_by_distance");
foreach ($stores as $store) {
echo $store->name . " (" . number_format($store->distance, 2) . "miles away)\n";
}
Let me know if you have any questions, and I hope you all enjoy the new feature.
It looks like you're new here. If you want to get involved, click one of these buttons!