seth
Nov 12th, 2007
Nov 12th, 2007
State name >> abbreviation
This is a quick write up for walking through a result set data and converting state names to abbreviations:
/** * Walks through an array and looks for a specific state key. It then tries to convert the full state name to its abbreviation. * Originally written for importing CSV data to the database where all of the rows contained full length state names. Thought it would * be useful in other functions so added it to global. Call it like this: array_walk($arr,'state_abbr',[state_key_name]);/sa * @uses array $states - Must have a states array formatted with abbreviations as the key. * @param array $arr - reference to the array we are walking through * @param string $key - current key * @param string $array_key - state field name string we should be checking for in array * @return void */ function state_abbr(&$arr,$key,$array_key){ global $states; if(strlen($arr[$array_key])==2 && array_key_exists(strtoupper($arr[$array_key]),$states)){ return false; }; //if the value is already a abbr, don't do anything $arr[$array_key] = array_search(ucwords(strtolower(trim($arr[$array_key]))),$states); } /* Don't forget your states array */ array('AL'=>'Alabama','AK'=>'Alaska');