Home > Repository > Country Array

Country Array

March 29th, 2009

Here is an easy to use function to allow you to display a list of countries to your users – this avoids them having to type it out (potentially incorrectly), and is ideal for use in a select field (drop-down box).

As an added bonus, you can also download a collection of flag images, with each image filename renamed to match the corresponding array key. The original images can be found at famfamfam.

Note: there may be a couple of countries in the array that don’t have a corresponding flag image, and vice-versa.

Downloads

Country Flags (146.74 KB — ZIP file)
Country Array (2.41 KB — ZIP file)

function country_array ($id = null)
{
    $list = array
    (
        0 => "Afghanistan",
        1 => "Albania",
        2 => "Algeria",
        3 => "American Samoa",
        4 => "Andorra",
        5 => "Angola",
        6 => "Anguilla",
        7 => "Antarctica",
        // ... snip ...
        230 => "Venezuela",
        231 => "Vietnam",
        232 => "Virgin Islands, British",
        233 => "Virgin Islands, U.S.",
        234 => "Wallis and Futuna",
        235 => "Western Sahara",
        236 => "Yemen",
        237 => "Zambia",
        238 => "Zimbabwe"
    );
 
    if (is_null($id))
    {
        return $list;
    }
    else
    {
        return $list[$id];
    }
}

Usage:

country_list(); // returns the entire array
country_list(224); // returns "United Kingdom"

Nick Repository

  1. No comments yet.
  1. No trackbacks yet.