Skip to content

Instantly share code, notes, and snippets.

@ZhukV
Created January 9, 2014 06:59
Show Gist options
  • Save ZhukV/8330442 to your computer and use it in GitHub Desktop.
Save ZhukV/8330442 to your computer and use it in GitHub Desktop.
Grouping countries by continent
<?php
namespace Acme\Demo;
/**
* Parsed from http://timezone.help.ch/
*
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
class CountryContinent
{
/**
* @var array
*/
private static $continents = array(
'AF' => 'Africa',
'AN' => 'Antarctica',
'AS' => 'Asia',
'EU' => 'Europe',
'NA' => 'North America',
'OC' => 'Oceania',
'SA' => 'South America'
);
private static $continentCountries = array(
'AF' => array('DZ', 'AO', 'BJ', 'BW', 'BF', 'BI', 'CM', 'CV', 'CF', 'TD', 'KM', 'DJ', 'EG', 'GQ', 'ER', 'ET', 'GA', 'GM', 'GH', 'GN', 'GW', 'CI', 'KE', 'LS', 'LR', 'LY', 'MG', 'MW', 'ML', 'MR', 'MU', 'YT', 'MA', 'MZ', 'NA', 'NE', 'NG', 'CG', 'CD', 'RE', 'RW', 'SH', 'ST', 'SN', 'SC', 'SL', 'SO', 'ZA', 'SS', 'SD', 'SZ', 'TZ', 'TG', 'TN', 'UG', 'EH', 'ZM', 'ZW'),
'AN' => array('AQ', 'BV', 'TF', 'HM', 'GS'),
'AS' => array('AF', 'AM', 'AZ', 'BH', 'BD', 'BT', 'IO', 'BN', 'KH', 'CX', 'CC', 'GE', 'HK', 'IN', 'ID', 'IR', 'IQ', 'IL', 'JP', 'JO', 'KZ', 'KW', 'KG', 'LA', 'LB', 'MO', 'MY', 'MV', 'MN', 'MM', 'NP', 'KP', 'OM', 'PK', 'PS', 'CN', 'PH', 'QA', 'SA', 'SG', 'KR', 'LK', 'SY', 'TW', 'TJ', 'TH', 'TR', 'TM', 'AE', 'UZ', 'VN', 'YE'),
'EU' => array('AX', 'AL', 'AD', 'AT', 'BY', 'BE', 'BA', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FO', 'FI', 'FR', 'DE', 'GI', 'GR', 'GG', 'IS', 'IE', 'IM', 'IT', 'JE', 'XK', 'LV', 'LI', 'LT', 'LU', 'MK', 'MT', 'MD', 'MC', 'ME', 'NL', 'NO', 'PL', 'PT', 'HU', 'RO', 'RU', 'SM', 'RS', 'SK', 'SI', 'ES', 'SJ', 'SE', 'CH', 'UA', 'GB', 'VA'),
'NA' => array('AI', 'AG', 'AW', 'BS', 'BB', 'BZ', 'BM', 'BQ', 'VG', 'CA', 'KY', 'CR', 'CU', 'CW', 'DM', 'SV', 'GL', 'GD', 'GP', 'GT', 'HT', 'HN', 'JM', 'MQ', 'MX', 'MS', 'NI', 'PA', 'PR', 'BL', 'KN', 'LC', 'MF', 'PM', 'VC', 'SX', 'DO', 'TT', 'TC', 'VI', 'US'),
'OC' => array('AS', 'AU', 'CK', 'TL', 'FJ', 'PF', 'GU', 'KI', 'MH', 'FM', 'NR', 'NC', 'NZ', 'NU', 'NF', 'MP', 'PW', 'PG', 'PN', 'WS', 'SB', 'TK', 'TO', 'TV', 'UM', 'VU', 'WF'),
'SA' => array('AR', 'BO', 'BR', 'CL', 'CO', 'EC', 'FK', 'GF', 'GY', 'PY', 'PE', 'SR', 'UY', 'VE')
);
/**
* Get continents
*
* @return array
*/
public static function getContinents()
{
return self::$continents;
}
/**
* Get continent name
*
* @param string $key
* @return array
*/
public static function getContinentName($key)
{
$key = mb_strtoupper($key);
if (isset(self::$continents[$key])) {
return self::$continents[$key];
}
return null;
}
/**
* Get countries by continent
*
* @param string $continent
* @return array
*/
public static function getCountriesByContinent($continent)
{
$continent = mb_strtoupper($continent);
if (isset(self::$continentCountries[$continent])) {
return self::$continentCountries[$continent];
}
return array();
}
/**
* Get continent by country
*
* @param string $country Country code
* @return string|null
*/
public static function getContinentByCountry($country)
{
$country = mb_strtoupper($country);
foreach (self::$continentCountries as $continent => $countries) {
if (in_array($countries, $country)) {
return $continent;
}
}
return null;
}
}
@VincentSit
Copy link

Line 90 should be if (in_array($country, $countries)) {.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment