Reputation: 5513
It's a simple function that takes a zipcode as input, finds the zipcode in the correct array and returns the corresponding city.
function zipcode_search($a){
$zip = (int)$a;
//arrays of zipcodes to corresponding cities
$FtMyers = array(33901,33902,33903,33905,33906,33907,33908,33911,33912,33913,33916,33917,33918,33919,33965,33966,33967,33990,33993,33994);
$Naples = array(34101,34102,34103,34104,34105,34106,34107,34108,34109,34110,34112,34113,34114,34116,34117,34119,34120);
$Cape = array(33904,33909,33910,33914,33915,33990,33991,33993);
$PtChar = array(33948,33949,33952,33953,33954,33980,33981,33983);
$Bonita = array(34133,34134,34135,34136);
$Marco = array(34145,34146);
$Estero = array(33928,33929);
$FMBeach = array(33931,33932);
$Sanibel = array(33957);
if (in_array($zip, $Naples))
{
return "Naples, FL";
}
elseif (in_array($zip, $Marco))
{
return "Marco Island, FL";
}
elseif (in_array($zip, $Bonita))
{
return "Bonita Springs, FL";
}
elseif (in_array($zip, $Estero))
{
return "Estero, FL";
}
elseif (in_array($zip, $FtMyers))
{
return "Fort Myers, FL";
}
elseif (in_array($zip, $FMBeach))
{
return "Ft. Myers Beach, FL";
}
elseif (in_array($zip, $Cape))
{
return "Cape Coral, FL";
}
elseif (in_array($zip, $Sanibel))
{
return "Sanibel Island, FL";
}
elseif (in_array($zip, $PtChar))
{
return "Port Charlotte, FL";
}
else { return "Florida";}
}
I know I can make this better and simultaneously improve the efficacy of it.
Upvotes: 0
Views: 69
Reputation: 12837
IMHO Ignacio Vazquez-Abrams' solution would be difficult to maintain -- there's a lot of copy & pasting going on there. Here's an alternative, which is similar to what you had originally:
function zipcode_search($zip){
$city_zips = array(
"Naples, FL" => array(34101,34102,34103,34104,34105,34106,34107,34108,34109,34110,34112,34113,34114,34116,34117,34119,34120),
"Marco Island, FL" => array(34145,34146),
// etc
);
foreach( $city_zips as $city => $zips ){
if( in_array($zip, $zips) ){
return $city;
}
}
}
Upvotes: 2
Reputation: 14237
function zipcode_search($zip){
$zip_array['Fort Myers, FL'] = array(33901,33902,33903,33905,33906,33907,33908,33911,33912,33913,33916,33917,33918,33919,33965,33966,33967,33990,33993,33994);
//... continue....
foreach($zip_array as $city => $zips){
if(in_array($zip, $zips)){
return $city;
}
}
}
Upvotes: 1
Reputation: 798636
$pcodes = array(33901 => "Fort Myers, FL", 33902 => "Fort Myers, FL",
33903 => "Fort Myers, FL", ...);
if (isset($pcodes[$zip]))
{
return $pcodes[$zip];
}
else
{
return 'Florida';
}
Upvotes: 2