Array of Countries, Indexed by Continent

For a recent commit, I needed an array of 2-digit countries codes split up by their continent. There’s some examples already out there, but I wasn’t too confident about their accuracy, so I ended up writing my own.

Here it is in case it’s useful to anyone else.

/**
 * Get a list of ISO-3166-2 countries by continent.
 *
 * Data was sourced from https://dev.maxmind.com/geoip/legacy/codes/country_continent/ on 2020-04-17.
 *
 * @param string $continent
 *
 * @return array
 */
function get_iso_3166_2_country_codes( $continent = '' ) {
	$codes = array(
		'antarctica' => array( 'AQ', 'BV', 'GS', 'HM', 'TF' ),

		'africa' => array(
			'AO', 'BF', 'BI', 'BJ', 'BW', 'CD', 'CF', 'CG', 'CI', 'CM', 'CV', 'DJ', 'DZ', 'EG', 'EH', 'ER', 'ET',
			'GA', 'GH', 'GM', 'GN', 'GQ', 'GW', 'KE', 'KM', 'LR', 'LS', 'LY', 'MA', 'MG', 'ML', 'MR', 'MU', 'MW',
			'MZ', 'NA', 'NE', 'NG', 'RE', 'RW', 'SC', 'SD', 'SH', 'SL', 'SN', 'SO', 'ST', 'SZ', 'TD', 'TG', 'TN',
			'TZ', 'UG', 'YT', 'ZA', 'ZM', 'ZW',
		),

		'asia' => array(
			// Includes the Middle East, Armenia, Azerbaijan, Cyprus, Georgia.
			'AE', 'AF', 'AM', 'AP', 'AZ', 'BD', 'BH', 'BN', 'BT', 'CC', 'CN', 'CX', 'CY', 'GE', 'HK', 'ID', 'IL',
			'IN', 'IO', 'IQ', 'IR', 'JO', 'JP', 'KG', 'KH', 'KP', 'KR', 'KW', 'KZ', 'LA', 'LB', 'LK', 'MM', 'MN',
			'MO', 'MV', 'MY', 'NP', 'OM', 'PH', 'PK', 'PS', 'QA', 'SA', 'SG', 'SY', 'TH', 'TJ', 'TL', 'TM', 'TW',
			'UZ', 'VN', 'YE',
		),

		'europe' => array(
			// Includes Russia, Turkey.
			'AD', 'AL', 'AT', 'AX', 'BA', 'BE', 'BG', 'BY', 'CH', 'CZ', 'DE', 'DK', 'EE', 'ES', 'EU', 'FI', 'FO',
			'FR', 'FX', 'GB', 'GG', 'GI', 'GR', 'HR', 'HU', 'IE', 'IM', 'IS', 'IT', 'JE', 'LI', 'LT', 'LU', 'LV',
			'MC', 'MD', 'ME', 'MK', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO', 'RS', 'RU', 'SE', 'SI', 'SJ', 'SK', 'SM',
			'TR', 'UA', 'VA',
		),

		'north america' => array(
			'AG', 'AI', 'AN', 'AW', 'BB', 'BL', 'BM', 'BS', 'BZ', 'CA', 'CR', 'CU', 'DM', 'DO', 'GD', 'GL', 'GP',
			'GT', 'HN', 'HT', 'JM', 'KN', 'KY', 'LC', 'MF', 'MQ', 'MS', 'MX', 'NI', 'PA', 'PM', 'PR', 'SV', 'TC',
			'TT', 'US', 'VC', 'VG', 'VI',
		),

		'oceania' => array(
			'AS', 'AU', 'CK', 'FJ', 'FM', 'GU', 'KI', 'MH', 'MP', 'NC', 'NF', 'NR', 'NU', 'NZ', 'PF', 'PG', 'PN',
			'PW', 'SB', 'TK', 'TO', 'TV', 'UM', 'VU', 'WF', 'WS',
		),

		'south america' => array(
			'AR', 'BO', 'BR', 'CL', 'CO', 'EC', 'FK', 'GF', 'GY', 'PE', 'PY', 'SR', 'UY', 'VE',
		),
	);

	if ( $continent ) {
		return $codes[ $continent ];
	} else {
		return $codes;
	}
}Code language: PHP (php)

One thought on “Array of Countries, Indexed by Continent

Leave a Reply

Your email address will not be published. Required fields are marked *