-4

我有以下代码。它是循环内的 If 语句的集合。if 语句将字符串(从 API 的 JSON 获得)与预定义的文本进行比较。问题是“If ==”比较仅在第一次通过循环时起作用。它在循环中的后续时间不起作用。当我回显文本时,它看起来与我正在比较的文本相同。

$vstate = (string)$vstate;
$vstate = trim($vstate);

//THERE IS A PROBLEM HERE ON THE SECOND LOOP
if($vstate == "Alabama") {$fstate = "01";}
if($vstate == "Alaska") {$fstate = "02";}
if($vstate == "Arizona") {$fstate = "04";}
if($vstate == "Arkansas") {$fstate = "05";}
if($vstate == "California") {$fstate = "06";}
if($vstate == "Colorado") {$fstate = "08";}
if($vstate == "Connecticut") {$fstate = "09";}
if($vstate == "Delaware") {$fstate = "10";}
if($vstate == "District of Columbia") {$fstate = "11";}
if($vstate == "Florida") {$cstate = "12";}
if($vstate == "Georgia") {$cstate = "13";}
if($vstate == "Hawaii") {$cstate = "15";}
if($vstate == "Idaho") {$cstate = "16";}
if($vstate == "Illinois") {$cstate = "17";}
if($vstate == "Indiana") {$cstate = "18";}
if($vstate == "Iowa") {$cstate = "19";}
if($vstate == "Kansas") {$cstate = "20";}
if($vstate == "Kentucky") {$cstate = "21";}
if($vstate == "Louisiana") {$cstate = "22";}
if($vstate == "Maine") {$cstate = "23";}
if($vstate == "Maryland") {$cstate = "24";}
if($vstate == "Massachusetts") {$cstate = "25";}
if($vstate == "Michigan") {$cstate = "26";}
if($vstate == "Minnesota") {$cstate = "27";}
if($vstate == "Mississippi") {$cstate = "28";}
if($vstate == "Missouri") {$cstate = "29";}
if($vstate == "Montana") {$cstate = "30";}
if($vstate == "Nebraska") {$cstate = "31";}
if($vstate == "Nevada") {$cstate = "32";}
if($vstate == "New Hampshire") {$cstate = "33";}
if($vstate == "New Jersey") {$cstate = "34";}
if($vstate == "New Mexico") {$cstate = "35";}
if($vstate == "New York") {$cstate = "36";}
if($vstate == "North Carolina") {$cstate = "37";}
if($vstate == "North Dakota") {$cstate = "38";}
if($vstate == "Ohio") {$cstate = "39";}
if($vstate == "Oklahoma") {$cstate = "40";}
if($vstate == "Oregon") {$cstate = "41";}
if($vstate == "Pennsylvania") {$cstate = "42";}
if($vstate == "Rhode Island") {$cstate = "44";}
if($vstate == "South Carolina") {$cstate = "45";}
if($vstate == "South Dakota") {$cstate = "46";}
if($vstate == "Tennessee") {$cstate = "47";}
if($vstate == "Texas") {$cstate = "48";}
if($vstate == "Utah") {$cstate = "49";}
if($vstate == "Vermont") {$cstate = "50";}
if($vstate == "Virginia") {$cstate = "51";}
if($vstate == "Washington") {$cstate = "53";}
if($vstate == "West Virginia") {$cstate = "54";}
if($vstate == "Wisconsin") {$cstate = "55";}
if($vstate == "Wyoming") {$cstate = "56";}
if($vstate == "Puerto Rico") {$cstate = "72";}
if($vstate == "American Samoa") {$cstate = "60";}
if($vstate == "Federated States of Micronesia") {$cstate = "64";}
if($vstate == "Guam") {$cstate = "66";}
if($vstate == "Marshall Islands") {$cstate = "68";}
if($vstate == "Commonwealth of the Northern Mariana Islands") {$cstate = "69";}
if($vstate == "Palau") {$cstate = "70";}
if($vstate == "Puerto Rico") {$cstate = "72";}
if($vstate == "U.S. Minor Outlying Islands") {$cstate = "74";}
if($vstate == "U.S. Virgin Islands") {$cstate = "78";}
if($vstate == "Baker Island") {$cstate = "81";}
if($vstate == "Howland Island") {$cstate = "84";}
if($vstate == "Jarvis Island") {$cstate = "86";}
if($vstate == "Johnston Atoll") {$cstate = "67";}
if($vstate == "Kingman Reef") {$cstate = "89";}
if($vstate == "Midway Islands") {$cstate = "71";}
if($vstate == "Navassa Island") {$cstate = "76";}
if($vstate == "Palmyra Atoll") {$cstate = "95";}
if($vstate == "Wake Island") {$cstate = "79";}

echo("The vstate is " . $vstate . "<br>");  
echo("The cstate is " . $cstate . "<br>");

我该如何进行这项工作?我已经尝试添加 Trim() 但这并没有解决问题。

谢谢

4

1 回答 1

0

$fstate 和 $cstate 需要在进入“循环”之前定义。它们都是在一个封装空间中创建的,并在 if 块结束时被删除。

如果你倾倒它,你会得到什么?它是否按预期正确转换为字符串?

于 2013-03-26T14:23:17.057 回答