我的sql为每个国家/地区保存一个号码。现在想要从数组中显示国家名称。
此问题是:如果我的国家/地区编号为11,则str_replace
显示'1' => 'Afghanistan',
两次。未显示11没有国家'11' => 'Armenia',
我的代码:
function mycountry($text){
$toReplace = array(
'1' => 'Afghanistan',
'7' => 'Anguilla',
'8' => 'Antarctica',
'9' => 'Antigua and Barbuda',
'10' => 'Argentina',
'11' => 'Armenia'
);
$replacement = array(
'1' => 'Afghanistan',
'7' => 'Anguilla',
'8' => 'Antarctica',
'9' => 'Antigua and Barbuda',
'10' => 'Argentina',
'11' => 'Armenia'
);
return str_replace($toReplace,$replacement,$text);
//return preg_replace('/$toReplace/','$replacement',$text);
}
// Sql query select user country
$country = 11;
echo ''.mycountry($country).'';
html display : AfghanistanAfghanistan
答案 0 :(得分:1)
删除数组键中的引号:
$toReplace = array(
1 => 'Afghanistan',
7 => 'Anguilla',
8 => 'Antarctica',
9 => 'Antigua and Barbuda',
10 => 'Argentina',
11 => 'Armenia'
);
并直接使用任何数组作为字典