2012-07-08 40 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"; 

} 
相關問題