2016-02-09 59 views
1

的價值我有一個數組搜索陣列刺痛,呼應匹配的關鍵

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

但上述回聲把國家(重點)不是國家代碼(值)

任何想法?

回答

1

怎麼樣只是用它作爲關鍵,如果情況是相同的:

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

或標準化的情況下,以匹配鍵:

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