cyberkiller Posted August 13, 2009 Share Posted August 13, 2009 I have a variable coming in with a state abbreviation which I want to change to the full name of the state. I am pretty new to php and have no clue how I should code it or where to start. I need to do it for all 50 states. I included some pseudo code below. $variable1 $variable2 if variable = la variable2 = los angeles if variable = ca variable2 = california etc.. for all 50 states. Link to comment https://forums.phpfreaks.com/topic/170091-solved-need-help-with-lookup-and-replace/ Share on other sites More sharing options...
JonnoTheDev Posted August 13, 2009 Share Posted August 13, 2009 use an array to map the values $states = array('la' => 'los angeles', 'ca' => 'california'); $x = "la"; print "State: ".$states[$x]; Link to comment https://forums.phpfreaks.com/topic/170091-solved-need-help-with-lookup-and-replace/#findComment-897283 Share on other sites More sharing options...
.josh Posted August 13, 2009 Share Posted August 13, 2009 // put in 'abbreviation' => 'state' for rest of states $states = array('tx' => 'texas','ca' => 'california','mo' => 'missouri'); // example $abbreviation = 'tx'; echo $states[$abbreviation]; p.s.- los angeles is not a state. Link to comment https://forums.phpfreaks.com/topic/170091-solved-need-help-with-lookup-and-replace/#findComment-897284 Share on other sites More sharing options...
JonnoTheDev Posted August 13, 2009 Share Posted August 13, 2009 oohhh 17 seconds too late Crayon Violent LOL Link to comment https://forums.phpfreaks.com/topic/170091-solved-need-help-with-lookup-and-replace/#findComment-897285 Share on other sites More sharing options...
.josh Posted August 13, 2009 Share Posted August 13, 2009 yeah i posted anyways though because i had to throw in the comment about la not being a state. Link to comment https://forums.phpfreaks.com/topic/170091-solved-need-help-with-lookup-and-replace/#findComment-897286 Share on other sites More sharing options...
PFMaBiSmAd Posted August 13, 2009 Share Posted August 13, 2009 Edit: same concept as above with some letter case handling and error checking You would use an array - <?php $lookup = array(); // use lowercase index names so that you can deal with any combination entered by converting it to lowercase $lookup['ca'] = "California"; $lookup['co'] = "Colorado"; // add rest of entries // $state contains the abbreviation you want to lookup $state = 'CA'; $state = strtolower($state); // convert to lower case in case it was entered uc or a mix of uc/lc if(isset($lookup[$state])){ echo $lookup[$state]; } else { echo "Not found<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/170091-solved-need-help-with-lookup-and-replace/#findComment-897292 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.