2012-07-08 38 views
0

我从数据库中获取我无法控制的信息。 “状态”的值是用户输入的(并且被正确清理的)值,但可以是写出的状态名称或两个字母的邮政缩写。我可以轻松地构建一个状态和缩写的关联数组。但我想知道是否有一种方法,PHP来确定一个值是否在状态数组中/缩写为。所以,如果你输入“CA”,它会看到它是一个有效的两个字母并返回它。如果它看到“XY”不是有效的那么它会回退默认的“OTHER”键(ZZ),但是如果用户输入的输入是“New York”,它将看到它是有效的并返回关联密钥“NY”?如果输入的数据是关键值,则检查并返回关联数组中的键值

+0

问题是? *(在很长的段落之后键入一个问号不会将其变成问题)* – alfasin 2012-07-08 09:31:15

+0

我没有降低您的问题。 – alfasin 2012-07-11 10:48:01

回答

3
$userInput; // Your user's input, processed using regex for capitals, etc to match DB values for the strings of the states. 
// Otherwise, do your comparisons in the conditions within the loop to control for mismatching capitals, etc. 

$output = false; 

foreach ($stateArray as $abbreviation => $full) // Variable $stateArray is your list of Abbreviation => State Name pairs. 
{ 
    if ($userInput == $abbreviation || $userInput == $full) // Use (strtolower($userInput) == strtolower($abbreviation) || strtolower($userInput) == strtolower($full)) to change all the comparison values to lowercase. 
    // This is one example of processing the strings in a way to ensure some flexibility in the user input. 
    // However, whatever processing you need to do is determined by your needs. 
    { 
     $output = array($abbreviation => $full); // If you want a key => value pair, use this. 
     $output = $abbreviation; // If you only want the key, use this instead. 
     break; 
    } 
} 

if ($output === false) 
{ 
    $output = array("ZZ" => "OTHER"); // If you want a key => value pair, use this. 
    $output = "ZZ"; // If you only want the key, use this instead. 
} 

编辑:我已经改变了循环,这样它会检查用户输入对的缩写和完整的国家名称在一种情况下,而不是让他们分开。

1

请与各国和缩写的数组:检查输入

$array = array("new york" => "ny", "california" => "ca", "florida" => "fl", "illinois" => "il"); 

$input = "nY"; 
if(strlen($input) == 2) // it's an abbreviation 
{ 
    $input = strtolower($input); // turns "nY" into "ny" 
    $state = array_search($input, $array); 
    echo $state; // prints "new york" 
    echo ucwords($state); // prints "New York" 
} 

// ----------------------------------------------------// 

$input = "nEw YoRk"; 
if(strlen($input) > 2) // it's a full state name 
{ 
    $input = strtolower($input); // turns "nEw YoRk" into "new york" 
    $abbreviation = $array[$input]; 
    echo $abbreviation; // prints "ny"; 
    echo strtoupper($abbreviation); // prints "NY" 
} 
+0

我会建议一个改进:将所有东西都变成小写(或大写),因为“纽约”和“纽约”应该返回相同的值。 +1 – alfasin 2012-07-08 09:32:44

+0

@alfasin完成:) – 2012-07-08 09:37:52

-1
if (!isset($array[$input])) 
{ 
    // swap it 
    $temp = array_flip($array); 

    if (isset($temp[$input])) 
    { 
    echo 'Got it as abbreviation!'; 
    } 
    else 
    { 
    echo 'NO Match'; 
    } 
} 
else 
{ 
    echo 'Got it as state!'; 
} 
+0

你犯了一个语法错误:'echo'NO匹配“'应该是'echo'NO Match'' – 2012-07-08 09:38:47

+0

你没有注意到。 – 2012-07-08 09:41:21

1
$array = array("New York" => "NY", 
"California" => "CA", 
"Florida" => "FL", 
"Illinois" => "IL"); 

$incoming = "New York"; 

if( in_array($incoming, $array) || array_key_exists($incoming, $array)){ 

echo "$incoming is valid"; 

} 
相关问题