替换国家名称表格编号


Replace country name form number

我的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

删除数组键中的引号:

$toReplace   = array(
1 => 'Afghanistan',
7 => 'Anguilla',
8 => 'Antarctica',
9 => 'Antigua and Barbuda',
10 => 'Argentina',
11 => 'Armenia'
);

并直接使用任何数组作为dictionary