PHP Script for States to Abbreviations Not Working Correctly

泄露秘密 提交于 2019-12-11 14:36:35

问题


I'm not sure what's wrong with this script. When I run it it only gives me the first letter of the abbreviation.

I didn't write the abbreviation part, I found that online. The only parts of the script that are mine are the GET and Includes.

I've set the state variable to a state and I still get the same thing so I know it's not my portion of the code. If anyone knows what the issue is please let me know.

Here is the updated code as suggested. Still having the same problem.

$state = 'Alabama';

function convert_state($name, $get = 'abbr') {
$states = array(
'Alabama'=>'AL',
'Alaska'=>'AK',
'Arizona'=>'AZ',
'Arkansas'=>'AR',
'California'=>'CA',
'Colorado'=>'CO',
'Connecticut'=>'CT',
'Delaware'=>'DE',
'Florida'=>'FL',
'Georgia'=>'GA',
'Hawaii'=>'HI',
'Idaho'=>'ID',
'Illinois'=>'IL',
'Indiana'=>'IN',
'Iowa'=>'IA',
'Kansas'=>'KS',
'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'=>'TN',
'Texas'=>'TX',
'Utah'=>'UT',
'Vermont'=>'VT',
'Virginia'=>'VA',
'Washington'=>'WA',
'West Virginia'=>'WV',
'Wisconsin'=>'WI',
'Wyoming'=>'WY'
);
if($get == 'name') {
// in this case $name is actually the abbreviation of the state name and you want the full name
$states = array_flip($states);
}

return $states[$name];
}

回答1:


There's an excellent answer to this question already, but since the OP is still having problems with this, here's an alternate solution. Please note that the array $a2s needs to be fully populated, as I only added two states to keep this short. Save the following to the file of your choice (eg: state.php):

<?php
$state = $_GET['state'];
echo convert_state($state);

function convert_state($key) {
    $a2s = array( 
        'AL'=>'Alabama',
        'CA'=>'California'
    );
    $array = (strlen($key) == 2 ? $a2s : array_flip($a2s));
    return $array[$key];
}
?>

To run the above, you'd type one of the following in your browser (assuming your file is state.php):

state.php?state=AL
state.php?state=Alabama

The code is designed to work either way.




回答2:


You should refactor your $states as:

$states = array(
  'Alabama' => 'AL',
  'Alaska' => 'AK',
  [...]
);

Then your function becomes:

function convert_state($name, $get = 'abbr') {
  $states = [.. see above ..];

  if($get == 'name') {
    // in this case $name is actually the abbreviation of the state name and you want the full name
    $states = array_flip($states);
  }

  return $states[$name];
}


来源:https://stackoverflow.com/questions/13728188/php-script-for-states-to-abbreviations-not-working-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!