Adrian World Design
  • Services
    • Website Consulting
    • Startup Consulting
    • Zend Development
  • Portfolio
    • My Framework
  • Knowledge
    • Web Standards
    • PHP bare-bones
    • Zend Framework
    • Zend Framework 2
    • Git - Github
    • Search Engine Optimization
    • Web Hosting
    • Linux
    • Microsoft Windows
    • Web Browsers
    • Mobile Devices
  • About
    • Business
    • Professionally
    • Personally
  • Contact
    • Contact Form
    • Phone
    • Email
    • Messaging

Knowledge Base Overview

Array List with Countries

Knowledge ⇒ Zend Framework ⇒ Helpful Tips ⇒ Array List with Countries
Tweet
Share on Tumblr

Created: Oct 9, 2011, 3:44:17 PM CDTLast updated: Jul 22, 2012, 12:56:40 PM CDT

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.

Quick Answers

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:

  1. 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.
  2. 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.
  3. 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.

Language List

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.

Final Note

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.

blog comments powered by Disqus
Prev
Next

Powered by FeedBurner Load our "Knowledge Base" feed in your RSS feeder

Follow us on Twitter
Follow us on Facebook
Follow us on LinkedIn
Follow us on Google+

All rights reserved, Adrian World Design ©2009–2022 Powered by Wejas Framework

Jump to Top