在数组中搜索字符串并回显匹配键的值

Search array for sting and echo the value of the matched key

我有一个数组

$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);

但是上面的回声把国家(键)而不是国家代码(值)

有什么想法吗?

如果大小写相同,只用它作为键怎么样:

echo $europe[$_GET["site_search"]];

或标准化大小写以匹配键:

$key = ucfirst(strtolower($_GET["site_search"]));
echo $europe[$key];