我有一个数组
$europe = array("Albania" => "AL","Andorra" => "AD","Austria" => "AT","Belarus" => "BY","Belgium" => "BE","Bosnia and Herzegovina" => "BA","Bulgaria" => "BG","Croatia" => "HR","Cyprus" => "CY",
"Czech Republic" => "CZ","Denmark" => "DK","Estonia" => "EE","Faroe Islands" => "FO","Finland" => "FI","France" => "FR","Germany" => "DE","Gibraltar" => "GI","Greece" => "GR","Hungary" => "HU","Iceland" => "IS","Ireland" => "IE","Italy" => "IT","Latvia" => "LV","Liechtenstein" => "LI","Lithuania" => "LT","Luxembourg" => "LU","Macedonia" => "MK","Malta" => "MT","Moldova" => "MD","Monaco" => "MC","Netherlands" => "NL","Norway" => "NO","Poland" => "PL","Portugal" => "PT","Romania" => "RO","Russia" => "RU","San Marino" => "SM","Serbia" => "RS","Slovakia" => "SK","Slovenia" => "SI","Spain" => "ES","Sweden" => "SE","Switzerland" => "CH","Ukraine" => "UA","United Kingdom" => "GB","Vatican city" => "VA","Yugoslavia" => "RS","Isle of Man" => "IM","Kosovo" => "RS","Montenegro" => "ME");
我希望在数组中搜索国家/地区的字符串匹配并检索国家/地区代码
$search = $_GET["site_search"];
foreach ($europe as $country => $cc){
if (strtolower($country) == $search){
echo $cc;
}
}
但我不认为上述内容是有效的,因为它循环并检查每个密钥。
所以我考虑过使用array_search ..
echo array_search($search,$europe,true);
但上述回声将国家/地区(关键)设为国家/地区代码(值)
有什么想法吗?
答案 0 :(得分:1)
如果情况相同,那么只使用它作为关键如何:
echo $europe[$_GET["site_search"]];
或规范化大小写以匹配键:
$key = ucfirst(strtolower($_GET["site_search"]));
echo $europe[$key];