Sorting the Country List
Created: Last updated:
If you are looking for only a list with countries sorted by continents or regions ready for download you should visit my Countries List project web site.
With the other website you don't need to know anything about programming. You can generate and download lists with countries sorted by continent or region in a few formats and ready to go in your project. Even with translations in many languages.
This webpage is mainly about programming in PHP and how to sort a list from Zend_Locale in the Zend Framework library.
This document is a tutorial complementing the document Array List with Countries and tells you how to sort the country list we receive with Zend_Locale from the Zend Framework.
The list we get with that method has a little shortcoming, though. The list is wildly sorted and you have no option for sorting the countries in the array by continent. Well, here is help and again: Welcome, to the power of Zend_Locale.
If you have not read the other document and don't know how to get the country list, read Array List with Countries first. I am not going to repeat certain things; you have been warned!
Territory versus Region
The secret to solve our riddle are two other $type arguments named TerritoryToRegion and the upsyturvy RegionToTerritory.
Before we go any further we have to understand two things here. What is a territory and what a region.
What is a territory?
Territory roughly means a country but also other things. It is not exact science.
What is a region?
Also not exact science but regions are collections of countries or other regions. The problems is here that regions can have children or, of course, parents. The top dog is  => World.
Why those names? Look at the name for the Zend class: it is named Zend_Locale. The main purpose of Zend_Locale is not providing the country list, that is just a byproduct. Zend_Locale offers many lists about many things which are grouped in regions and territories. It just so happens that a country is grouped in one or more regions but also acts as a territory. If you like to know more about Zend_Locale here is the official documentation.
You can get a list with everything except countries the same way as the country list described in the other document. Simply use a different $value argument
- // use 1 as the $value argument
- $territoryArray = Zend_Locale::getTranslationList('Territory','en_US',1);
- // and this is what you get
- Array (  => World  => Africa  => North America  => South America  => Oceania  => Western Africa  => Central America  => Eastern Africa  => Northern Africa  => Middle Africa  => Southern Africa  => Americas  => Northern America  => Caribbean  => Eastern Asia  => Southern Asia  => South-Eastern Asia  => Southern Europe  => Australia and New Zealand  => Melanesia  => Micronesian Region  => Polynesia  => South-Central Asia  => Asia  => Central Asia  => Western Asia  => Europe  => Eastern Europe  => Northern Europe  => Western Europe  => Commonwealth of Independent States  => Czechoslovakia  => Latin America and the Caribbean  => Channel Islands [QO] => Outlying Oceania [QU] => European Union )
Linking Regions to Territories
Now lets get everything together.
Our country list from the other page was pulled with type Territory and value 2. What we have to do next is pull basically the same list again but this time with TerritoryToRegion. Instead of a list with names we have the countries (plus everything else) with their associated region. With RegionToTerritory we have more or less the same just the other way around.
Once we have all our lists with countries, regions, territories and some understand about all the terms we can finally go to work and link our countries to the appropriate regions. Which is a little bit tricky.
Lets take for example Mexico [MX]. When we look at the list from TerritoryToRegion we see MX => 013 and looking at the regions list this is  => Central America. Looking at the RegionToTerritory list we see that 013 is in a region  => Americas but also  => North America and  => Latin America and the Caribbean.
If you look around some more you will find all sorts of combinations which leads me to say: Do whatever you want, the possibilities are almost endless.
You can create a great amount of sorting here in almost any way possible. The only thing you need to look out for are duplicates because some countries are listed directly in various regions but through inheritance even more. Ultimately they are all linked to the top dog  => World!