Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
US State Names & Abbreviations as PHP Arrays
<?php
/* From https://www.usps.com/send/official-abbreviations.htm */
$us_state_abbrevs_names = array(
'AL'=>'ALABAMA',
'AK'=>'ALASKA',
'AS'=>'AMERICAN SAMOA',
'AZ'=>'ARIZONA',
'AR'=>'ARKANSAS',
'CA'=>'CALIFORNIA',
'CO'=>'COLORADO',
'CT'=>'CONNECTICUT',
'DE'=>'DELAWARE',
'DC'=>'DISTRICT OF COLUMBIA',
'FM'=>'FEDERATED STATES OF MICRONESIA',
'FL'=>'FLORIDA',
'GA'=>'GEORGIA',
'GU'=>'GUAM GU',
'HI'=>'HAWAII',
'ID'=>'IDAHO',
'IL'=>'ILLINOIS',
'IN'=>'INDIANA',
'IA'=>'IOWA',
'KS'=>'KANSAS',
'KY'=>'KENTUCKY',
'LA'=>'LOUISIANA',
'ME'=>'MAINE',
'MH'=>'MARSHALL ISLANDS',
'MD'=>'MARYLAND',
'MA'=>'MASSACHUSETTS',
'MI'=>'MICHIGAN',
'MN'=>'MINNESOTA',
'MS'=>'MISSISSIPPI',
'MO'=>'MISSOURI',
'MT'=>'MONTANA',
'NE'=>'NEBRASKA',
'NV'=>'NEVADA',
'NH'=>'NEW HAMPSHIRE',
'NJ'=>'NEW JERSEY',
'NM'=>'NEW MEXICO',
'NY'=>'NEW YORK',
'NC'=>'NORTH CAROLINA',
'ND'=>'NORTH DAKOTA',
'MP'=>'NORTHERN MARIANA ISLANDS',
'OH'=>'OHIO',
'OK'=>'OKLAHOMA',
'OR'=>'OREGON',
'PW'=>'PALAU',
'PA'=>'PENNSYLVANIA',
'PR'=>'PUERTO RICO',
'RI'=>'RHODE ISLAND',
'SC'=>'SOUTH CAROLINA',
'SD'=>'SOUTH DAKOTA',
'TN'=>'TENNESSEE',
'TX'=>'TEXAS',
'UT'=>'UTAH',
'VT'=>'VERMONT',
'VI'=>'VIRGIN ISLANDS',
'VA'=>'VIRGINIA',
'WA'=>'WASHINGTON',
'WV'=>'WEST VIRGINIA',
'WI'=>'WISCONSIN',
'WY'=>'WYOMING',
'AE'=>'ARMED FORCES AFRICA \ CANADA \ EUROPE \ MIDDLE EAST',
'AA'=>'ARMED FORCES AMERICA (EXCEPT CANADA)',
'AP'=>'ARMED FORCES PACIFIC'
);
?>
@bra1nDeaD
Copy link

bra1nDeaD commented Oct 14, 2021

In case you live in Europe, like me:

$states = array();

@j4nr6n
Copy link

j4nr6n commented Oct 15, 2021

State names, but without the vowels:

$stateNames = [
    'AL'=>'lbm',
    'AK'=>'lsk',
    'AS'=>'mrcn sm',
    'AZ'=>'rzn',
    'AR'=>'rknss',
    'CA'=>'Clfrn',
    'CO'=>'Clrd',
    'CT'=>'Cnnctct',
    'DE'=>'Dlwr',
    'DC'=>'Dstrct f Clmb',
    'FM'=>'Fdrtd Stts f Mcrns',
    'FL'=>'Flrd',
    'GA'=>'Grg',
    'GU'=>'Gm G',
    'HI'=>'Hw',
    'ID'=>'dh',
    'IL'=>'llns',
    'IN'=>'ndn',
    'IA'=>'w',
    'KS'=>'Knss',
    'KY'=>'Kntck',
    'LA'=>'Lsn',
    'ME'=>'Mn',
    'MH'=>'Mrshll slnds',
    'MD'=>'Mrlnd',
    'MA'=>'Msschstts',
    'MI'=>'Mchgn',
    'MN'=>'Mnnst',
    'MS'=>'Msssspp',
    'MO'=>'Mssr',
    'MT'=>'Mntn',
    'NE'=>'Nbrsk',
    'NV'=>'Nvd',
    'NH'=>'Nw Hmpshr',
    'NJ'=>'Nw Jrs',
    'NM'=>'Nw Mxc',
    'NY'=>'Nw rk',
    'NC'=>'Nrth Crln',
    'ND'=>'Nrth Dkt',
    'MP'=>'Nrthrn Mrn slnds',
    'OH'=>'h',
    'OK'=>'klhm',
    'OR'=>'rgn',
    'PW'=>'Pl',
    'PA'=>'Pnnslvn',
    'PR'=>'Prt Rc',
    'RI'=>'Rhd slnd',
    'SC'=>'Sth Crln',
    'SD'=>'Sth Dkt',
    'TN'=>'Tnnss',
    'TX'=>'Txs',
    'UT'=>'th',
    'VT'=>'Vrmnt',
    'VI'=>'Vrgn slnds',
    'VA'=>'Vrgn',
    'WA'=>'Wshngtn',
    'WV'=>'Wst Vrgn',
    'WI'=>'Wscnsn',
    'WY'=>'Wmng',
    'AE'=>'rmd Frcs frc \ Cnd \ rp \ Mddl st',
    'AA'=>'rmd Frcs mrc (xcpt Cnd)',
    'AP'=>'rmd Frcs Pcfc'
];

@emargareten
Copy link

emargareten commented Oct 15, 2021

@shellscape
Copy link

shellscape commented Oct 21, 2021

@humbletiger
Copy link

humbletiger commented Nov 27, 2021

Convert case insensitive long-form spelling into the abbreviation, or return original uppercase if no match (comedy not included).

function state($state_entry) {
	switch( strtolower($state_entry) ) {
		case "district of columbia": $state = "DC"; break;
		case "alaska": $state = "AK"; break;
		case "alabama": $state = "AL"; break;
		case "arkansas": $state = "AR"; break;
		case "arizona": $state = "AZ"; break;
		case "california": $state = "CA"; break;
		case "colorado": $state = "CO"; break;
		case "connecticut": $state = "CT"; break;
		case "delaware": $state = "DE"; break;
		case "florida": $state = "FL"; break;
		case "georgia": $state = "GA"; break;
		case "hawaii": $state = "HI"; break;
		case "iowa": $state = "IA"; break;
		case "idaho": $state = "ID"; break;
		case "illinois": $state = "IL"; break;
		case "indiana": $state = "IN"; break;
		case "kansas": $state = "KS"; break;
		case "kentucky": $state = "KY"; break;
		case "louisiana": $state = "LA"; break;
		case "massachusetts": $state = "MA"; break;
		case "maryland": $state = "MD"; break;
		case "maine": $state = "ME"; break;
		case "michigan": $state = "MI"; break;
		case "minnesota": $state = "MN"; break;
		case "missouri": $state = "MO"; break;
		case "mississippi": $state = "MS"; break;
		case "montana": $state = "MT"; break;
		case "north carolina": $state = "NC"; break;
		case "north dakota": $state = "ND"; break;
		case "nebraska": $state = "NE"; break;
		case "new hampshire": $state = "NH"; break;
		case "new jersey": $state = "NJ"; break;
		case "new mexico": $state = "NM"; break;
		case "nevada": $state = "NV"; break;
		case "new york": $state = "NY"; break;
		case "ohio": $state = "OH"; break;
		case "oklahoma": $state = "OK"; break;
		case "oregon": $state = "OR"; break;
		case "pennsylvania": $state = "PA"; break;
		case "rhode island": $state = "RI"; break;
		case "south carolina": $state = "SC"; break;
		case "south dakota": $state = "SD"; break;
		case "tennessee": $state = "TN"; break;
		case "texas": $state = "TX"; break;
		case "utah": $state = "UT"; break;
		case "virginia": $state = "VA"; break;
		case "vermont": $state = "VT"; break;
		case "washington": $state = "WA"; break;
		case "wisconsin": $state = "WI"; break;
		case "west virginia": $state = "WV"; break;
		case "wyoming": $state = "WY"; break;
		default: $state = $state_entry;
	}
	return strtoupper($state);
}

@iamkeir
Copy link

iamkeir commented Mar 25, 2022

WHY IS EVERYONE SHOUTING

$stateNames = [
'AL' => 'Alabama', 
'AK' => 'Alaska', 
'AS' => 'American Samoa', 
'AZ' => 'Arizona', 
'AR' => 'Arkansas', 
'AE' => 'Armed Forces - Europe', 
'AP' => 'Armed Forces - Pacific', 
'AA' => 'Armed Forces - USA/Canada', 
'CA' => 'California', 
'CO' => 'Colorado', 
'CT' => 'Connecticut', 
'DE' => 'Delaware', 
'DC' => 'District of Columbia', 
'FM' => 'Federated States of Micronesia', 
'FL' => 'Florida', 
'GA' => 'Georgia', 
'GU' => 'Guam', 
'HI' => 'Hawaii', 
'ID' => 'Idaho', 
'IL' => 'Illinois', 
'IN' => 'Indiana', 
'IA' => 'Iowa', 
'KS' => 'Kansas', 
'KY' => 'Kentucky', 
'LA' => 'Louisiana', 
'ME' => 'Maine', 
'MH' => 'Marshall Islands', 
'MD' => 'Maryland', 
'MA' => 'Massachusetts', 
'MI' => 'Michigan', 
'MN' => 'Minnesota', 
'MS' => 'Mississippi', 
'MO' => 'Missouri', 
'MT' => 'Montana', 
'NE' => 'Nebraska', 
'NV' => 'Nevada', 
'NH' => 'New Hampshire', 
'NJ' => 'New Jersey', 
'NM' => 'New Mexico', 
'NY' => 'New York', 
'NC' => 'North Carolina', 
'ND' => 'North Dakota', 
'OH' => 'Ohio', 
'OK' => 'Oklahoma', 
'OR' => 'Oregon', 
'PA' => 'Pennsylvania', 
'PR' => 'Puerto Rico', 
'RI' => 'Rhode Island', 
'SC' => 'South Carolina', 
'SD' => 'South Dakota', 
'TN' => 'Tennessee', 
'TX' => 'Texas', 
'UT' => 'Utah', 
'VT' => 'Vermont', 
'VI' => 'Virgin Islands', 
'VA' => 'Virginia', 
'WA' => 'Washington', 
'WV' => 'West Virginia', 
'WI' => 'Wisconsin', 
'WY' => 'Wyoming', 
];

@yomyyo
Copy link

yomyyo commented Mar 30, 2022

Reverse reverse

$statesToAbbr = array(
  'Alaska' => 'AK',
  'Alabama' => 'AL',
  'Arkansas' => 'AR',
  'Arizona' => 'AZ',
  'California' => 'CA',
  'Colorado' => 'CO',
  'Connecticut' => 'CT',
  'Delaware' => 'DE',
  'Florida' => 'FL',
  'Georgia' => 'GA',
  'Hawaii' => 'HI',
  'Idaho' => 'ID',
  'Illinois' => 'IL',
  'Indiana' => 'IN',
  'Iowa' => 'IA',
  'Kentucky' => 'KY',
  'Louisiana' => 'LA',
  'Maine' => 'ME',
  'Maryland' => 'MD',
  'Massachusetts' => 'MA',
  'Michigan' => 'MI',
  'Minnesota' => 'MN',
  'Mississippi' => 'MS',
  'Missouri' => 'MO',
  'Montana' => 'MT',
  'Nebraska' => 'NE',
  'Nevada' => 'NV',
  'New Hampshire' => 'NH',
  'New Jersey' => 'NJ',
  'New Mexico' => 'NM',
  'New York' => 'NY',
  'North Carolina' => 'NC',
  'North Dakota' => 'ND',
  'Ohio' => 'OH',
  'Oklahoma' => 'OK',
  'Oregon' => 'OR',
  'Pennsylvania' => 'PA',
  'Rhode Island' => 'RI',
  'South Carolina' => 'SC',
  'South Dakota' => 'SD',
  'Tennessee' => 'TX',
  'Texas' => 'AK',
  'Utah' => 'UT',
  'Vermont' => 'VT',
  'Virginia' => 'VA',
  'Washington' => 'WA',
  'West Virginia' => 'WV',
  'Wisconsin' => 'WI',
  'Wyoming' => 'WY',
);

@nikmclaughlin
Copy link

nikmclaughlin commented Apr 13, 2022

A PHP array of state names and abbreviations but every time there's an 'a' I insert the next lyric to All-Star by Smash Mouth

<?php

/* From https://pe.usps.com/text/pub28/28apb.htm && https://www.google.com/search?q=all+star+lyrics */

$us_state_all_star_names = array(
	'AsomebodyL'=>'AonceLAtoldBAmeMAthe',
	'AworldK'=>'AisLAgonnaSKAroll',
	'AmeS'=>'AiMERICAaintN SAtheMOAsharpest',
	'AtoolZ'=>'AinRIZONAthe',
	'AshedR'=>'AsheRKAwasNSAlookingS',
	'CAkind'=>'CAofLIFORNIAdumb',
	'CO'=>'COLORAwithDO',
	'CT'=>'CONNECTICUT',
	'DE'=>'DELAherWAfingerRE',
	'DC'=>'DISTRICT OF COLUMBIAand',
	'FM'=>'FEDERAherTED STAthumbTES OF MICRONESIAin',
	'FL'=>'FLORIDAthe',
	'GAshape'=>'GEORGIAof',
	'GU'=>'GUAanM GU',
	'HI'=>'HA"L"WAonII',
	'ID'=>'IDAherHO',
	'IL'=>'ILLINOIS',
	'IN'=>'INDIAforeheadNAwell',
	'IAthe'=>'IOWAyears',
	'KS'=>'KAstartNSAcomingS',
	'KY'=>'KENTUCKY',
	'LAand'=>'LOUISIAtheyNAdont',
	'ME'=>'MAstopINE',
	'MH'=>'MAcomingRSHAfedLL ISLAtoNDS',
	'MD'=>'MAtheRYLArulesND',
	'MAand'=>'MAiSSAhitCHUSETTS',
	'MI'=>'MICHIGAtheN',
	'MN'=>'MINNESOTAground',
	'MS'=>'MISSISSIPPI',
	'MO'=>'MISSOURI',
	'MT'=>'MONTArunningNAdidnt',
	'NE'=>'NEBRAmakeSKAsense',
	'NV'=>'NEVAnotDAto',
	'NH'=>'NEW HAliveMPSHIRE',
	'NJ'=>'NEW JERSEY',
	'NM'=>'NEW MEXICO',
	'NY'=>'NEW YORK',
	'NC'=>'NORTH CAforROLINAfun',
	'ND'=>'NORTH DAyourKOTAbrain',
	'MP'=>'NORTHERN MAgetsRIAsmartNAbut ISLAyourNDS',
	'OH'=>'OHIO',
	'OK'=>'OKLAheadHOMAgets',
	'OR'=>'OREGON',
	'PW'=>'PAdumbLAsoU',
	'PAmuch'=>'PENNSYLVAtoNIAdo',
	'PR'=>'PUERTO RICO',
	'RI'=>'RHODE ISLAsoND',
	'SC'=>'SOUTH CAmuchROLINAto',
	'SD'=>'SOUTH DAseeKOTAso',
	'TN'=>'TENNESSEE',
	'TX'=>'TEXAwhatsS',
	'UT'=>'UTAwrongH',
	'VT'=>'VERMONT',
	'VI'=>'VIRGIN ISLAwithNDS',
	'VAtaking'=>'VIRGINIAthe',
	'WAback'=>'WAstreetsSHINGTON',
	'WV'=>'WEST VIRGINIAyoull',
	'WI'=>'WISCONSIN',
	'WY'=>'WYOMING',
	'AneverE'=>'AknowRMED FORCES AifFRICAyou \ CAdontNAgoDAyoull \ EUROPE \ MIDDLE EAneverST',
	'AshineAif'=>'AyouRMED FORCES AdontMERICAglow (EXCEPT CAheyNAnowDAyoure)',
	'AanP'=>'AallRMED FORCES PAstarCIFIC'
);

?>

@technoken35
Copy link

technoken35 commented May 6, 2022

This is not accurate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment