В вашем коде есть ошибка с определением $ countryArray.
')' опущено в конце определения массива.
И '>' в конце определения следует удалить.
Проверьте это.
$countryArray = array(
'AD'=>array('name'=>'ANDORRA','code'=>'376'),
'AE'=>array('name'=>'UNITED ARAB EMIRATES','code'=>'971'),
'AF'=>array('name'=>'AFGHANISTAN','code'=>'93'),
'AG'=>array('name'=>'ANTIGUA AND BARBUDA','code'=>'1268'),
'AI'=>array('name'=>'ANGUILLA','code'=>'1264'),
'AL'=>array('name'=>'ALBANIA','code'=>'355')
);
//function for foreach select
function countrySelector($defaultCountry = "", $id = "", $name = "", $classes= ""){
global $countryArray;
$output = "<select id='".$id."' name='".$name."' class='".$classes."'>";
foreach($countryArray as $code => $country){
$countryName = ucwords(strtolower($country["name"]));
$output .= "<option value='".$code."' ".(($code==strtoupper($defaultCountry))?"selected":"").">".$code." - ".$countryName." (+".$country["code"].")</option>";
}
$output .= "</select>";
return $output;
}
echo countrySelector('AF','my-country', 'my-country', 'form-control');
А вот полный исходный код.
<?php
$countryArray = array(
'AD'=>array('name'=>'ANDORRA','code'=>'376'),
'AE'=>array('name'=>'UNITED ARAB EMIRATES','code'=>'971'),
'AF'=>array('name'=>'AFGHANISTAN','code'=>'93'),
'AG'=>array('name'=>'ANTIGUA AND BARBUDA','code'=>'1268'),
'AI'=>array('name'=>'ANGUILLA','code'=>'1264'),
'AL'=>array('name'=>'ALBANIA','code'=>'355')
);
//function for foreach select
function countrySelector($defaultCountry = "", $id = "", $name = "", $classes= ""){
global $countryArray;
$output = "<select id='".$id."' name='".$name."' class='".$classes."'>";
foreach($countryArray as $code => $country){
$countryName = ucwords(strtolower($country["name"]));
$output .= "<option value='".$code."' ".(($code==strtoupper($defaultCountry))?"selected":"").">".$code." - ".$countryName." (+".$country["code"].")</option>";
}
$output .= "</select>"; return $output;
}
echo countrySelector('AF','my-country', 'my-country', 'form-control');
?>