我從數據庫中獲取我無法控制的信息。 「狀態」的值是用戶輸入的(並且被正確清理的)值,但可以是寫出的狀態名稱或兩個字母的郵政縮寫。我可以輕鬆地構建一個狀態和縮寫的關聯數組。但我想知道是否有一種方法,PHP來確定一個值是否在狀態數組中/縮寫爲鍵或值。所以,如果你輸入「CA」,它會看到它是一個有效的兩個字母鍵並返回它。如果它看到「XY」不是有效的鍵那麼它會回退默認的「OTHER」鍵(ZZ),但是如果用戶輸入的輸入是「New York」,它將看到它是有效的值並返回關聯密鑰「NY」?如果輸入的數據是關鍵值,則檢查並返回關聯數組中的鍵值
回答
$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.
}
編輯:我已經改變了循環,這樣它會檢查用戶輸入對的縮寫和完整的國家名稱在一種情況下,而不是讓他們分開。
請與各國和縮寫的數組:檢查輸入
$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"
}
我會建議一個改進:將所有東西都變成小寫(或大寫),因爲「紐約」和「紐約」應該返回相同的值。 +1 – alfasin 2012-07-08 09:32:44
@alfasin完成:) – 2012-07-08 09:37:52
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!';
}
你犯了一個語法錯誤:'echo'NO匹配「'應該是'echo'NO Match'' – 2012-07-08 09:38:47
你沒有注意到。 – 2012-07-08 09:41:21
$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";
}
- 1. 在數組中搜索關鍵字並返回它的值?
- 2. 如果包含鍵值對,則返回數組中的對象
- 3. 關聯數組的數組關聯數組,索引於鍵的值
- 4. PHP搜索多維關聯數組的鍵和返回鍵=>值
- 5. 檢查關聯數組包含鍵值對
- 6. 以變量爲關鍵字檢索關聯數組值
- 7. 從關聯數組中添加值如果鍵匹配
- 8. 如何追加數組中的值,如果關鍵是相同
- 9. 在多維數組中搜索值並保持鍵的關聯
- 10. PHP陣列關聯數組添加鍵/值,檢查鍵存在,爲值數組添加值
- 11. PHP - 使關聯數組唯一,鍵 - >值和值 - >鍵
- 12. Javascript數組檢查關鍵
- 13. 獲取關聯數組的值,如果鍵的鍵或部分匹配
- 14. Redis(鍵/值數據庫)和關聯數組之間的區別?
- 15. 關聯數組的鍵名
- 16. 數組值找到關鍵
- 17. 檢查關聯數組包含值
- 18. 用php關聯數組檢查bool值
- 19. 檢查關聯數組中是否存在鍵
- 20. php:如何通過鍵更新關聯數組中的值
- 21. 如何從不完整的關聯數組中獲取鍵/值?
- 22. 如何從關聯數組中刪除鍵和它的值?
- 23. 如何獲得關聯數組中的鍵和值
- 24. PHP - 關聯數組中的回聲值
- 25. 檢查數組值中的另一個關聯數組
- 26. 與嵌套關聯數組中相同鍵關聯的值的總和
- 27. 如何顯示上Laravel而非關聯數組的值的鍵
- 28. 如何在PHP中的關聯數組中插入新的鍵值對?
- 29. 使用關聯數組鍵和數值數組值創建一個數組PHP
- 30. PHP - 合併兩個關聯數組(如果鍵存在增加值)
問題是? *(在很長的段落之後鍵入一個問號不會將其變成問題)* – alfasin 2012-07-08 09:31:15
我沒有降低您的問題。 – alfasin 2012-07-11 10:48:01