我正在编写一个php函数将ISO 3166-1 alpha 2代码转换为国家电话代码。我的挑战是,当我调用函数时,只显示+符号。我怎么才能让数字也出现呢?下面是我使用的代码,只是我减少了国家的数量。
<?php
function ctryarray($data)
{
$redata = "";
$country['AF'] = "+93";
$country['AL'] = "+355";
$country['DZ'] = "+213";
$country['AS'] = "+1";
$redata = $country[$data];
return $redata;
}
?>
//Then I use the following code to call it:
$countrycode = ctryarray($ccode);
其中$ccode为ISO 3166-1 alpha-2代码
虽然您提供的代码没有显示您所描述的错误,但它可能会被压缩一点,并且它还应该有一些错误检查:
function ctryarray($data) {
$country['AF'] = "+93";
$country['AL'] = "+355";
$country['DZ'] = "+213";
$country['AS'] = "+1";
if (array_key_exists($data,$country)) {
return $country[$data];
} else {
return false;
}
}