Array List with Countries
Created: Last updated:
Are you looking for a list with countries then please read on!
If you have a problem finding things like such lists here's a little tip. The list you are looking for should not have the words from your seach in it. Unfortunately, your search engine (like Google) will not find such lists by magic because they are matching the words in your search against webpages like this one. They find this page because I write about country lists but you have to read. So, again please read on because most likely following I have the links to the country list you are looking for.
I do have webpages with some data about continents, regions, territories and countries. I also have them in different formats like in a PHP array with the ISO abbreviation as the index key. I even have translations in multiple languages.
First I have a couple quick and dirty answers and then a more elaborate answer for those working with PHP whole to include a countries list into their website:
- For a simple countries list go to my Countries List website. There you can generate and download country lists not only sorted by continents or regions but also in different languages and formats.
- If you can't read or understand English and just scan the page for a download link then here is one:
DOWNLOAD COUNTRIES LIST
You should really read point 1 and select that link, though. The list in this link is only a unsorted simple countries list. No continents, regions, languages or formats like a PHP array or XML.
- If you need something more elaborate and versatile which fits and works in your PHP project you can use Zend_Locale with the territories list from the Zend Framework. In fact, that's what this page is about.
So, you can search the web for Zend_Locale and Zend Framework and help yourself or just read on. If you need more information about Zend_Locale is and how to pull a countries list please then this page is all about.
The power of Zend_Locale
If you don't know Zend Framework then a hearty welcome to Zend Framework and the power of Zend_Locale. If you feel comfortable with a large and somewhat complex application framework you will love Zend_Locale and what it can do for you in terms of country lists. Especially when you need the list in many different languages. In this little tutorial I will show you how to work with Zend_Locale and get a list with almost all the countries in this world in many different languages.
First some background
According to the documentation "Zend_Locale is the Frameworks answer to the question, "How can the same application be used around the whole world?" ... and some more. Besides all the main purposes in Zend_Locale there is this little hidden treasure which might be overlooked by most people. Zend_Locale uses an impressive amount of XML lists with all sorts of data and you are basically able to pull aggregated lists of this data. Including a list of all continents, territories and countries.
As with all Zend classes you don't need a full installation and implementation of the framework. You just need to make sure you get the path right. The Zend_Locale class can be used independently and most methods are implemented as static methods, i.e. you don't even have to create an object of the class.
The easy solution
To get a list from Zend_Locale you will use a method named "getTranslationList". You can pass three arguments:
- $type - This is the name for the list you want [required]
- $locale - The language you want [optional] - default is your browser/system default
- $value - Some types allow detailed lists which can be selected with this [optional]
This is almost all you need to know to get your list with all countries in the world. So, what are the values we need for the arguments? Lets begin with type where we say that we like to get the countries.
If you have guessed "country" you guessed wrong and will see an exception. Agreed, this is a bit confusing or lets say misleading and why it might be overlooked by many. The correct type for country is "Territory" and the value is "2". Without a value you will get the list with actual territories plus continents and countries. That might be too much, though. So, you will type this
- // just make sure you have the class available
- $countryArray = Zend_Locale::getTranslationList('Territory','en_US',2);
and you will get an array as the return with roughly 263 countries in English. Be warned, that other languages will have different results since the list gathers information from the XML files mentioned above. It all depends how complete (accurate) those lists are.
There are over 508 languages, mind you, and you can get that list the exact same way, just use type "language"; no value is required for that.
- // just use a different type
- $languageArray = Zend_Locale::getTranslationList('Language','en_US');
Sorting by continents or regions
If you are wondering now how to sort this list, e.g. by continent we have to dig deeper into Zend_Locale and other supported types. In the next tutorial document I will explain what types you need and how then to sort the country list by continents or regions.
A list with all the supported types for Zend_Locale can be found in the official documentation; search the page for getTranslationList.
Note that there have been some changes from earlier versions; this information should be accurate as of Version 1.10. Notably there used to be a type "country" which now throws an exception. There is still a method getCountryTranslationList but it is labeled as deprecated and actually triggers a E_USER_NOTICE error.