我从数据库中获取我无法控制的信息。 “状态”的值是用户输入的(并且被正确清理的)值,但可以是写出的状态名称或两个字母的邮政缩写。我可以轻松地构建一个状态和缩写的关联数组。但我想知道是否有一种方法,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