我们可以创造我们自己国家的数组,并使用CountryDropdownField
setSource
功能设定国家的顺序:
$countriesList = Zend_Locale::getTranslationList('territory', i18n::get_locale(), 2);
asort($countriesList, SORT_LOCALE_STRING);
$commonCountries = array(
'AU' => 'Australia',
'CA' => 'Canada',
'FR' => 'France',
'DE' => 'Germany',
'NZ' => 'New Zealand',
'ZA' => 'South Africa',
'GB' => 'United Kingdom',
'US' => 'United States'
);
CountryDropdownField::create('Country', 'Country')
->setSource(array_merge($commonCountries, $countriesList));
或使用uksort
代替合并阵列:
$countriesList = Zend_Locale::getTranslationList('territory', i18n::get_locale(), 2);
$commonCountries = array('AU', 'CA', 'FR', 'DE', 'NZ', 'ZA', 'GB', 'US');
uksort($countriesList, function ($a, $b) use ($countriesList, $commonCountries) {
if (in_array($a, $commonCountries)) {
if (in_array($b, $firstCountries)) {
return ($countriesList[$a] < $countriesList[$b]) ? -1 : 1;
}
return -1;
}
if (in_array($b, $commonCountries)) {
return 1;
}
return ($countriesList[$a] < $countriesList[$b]) ? -1 : 1;
});
CountryDropdownField::create('Country', 'Country')->setSource($countriesList);