liamjcooper/php-countries

Retrieve a list of countries to make working with international applications easier.

dev-main 2021-04-08 09:01 UTC

This package is not auto-updated.

Last update: 2024-10-05 00:30:31 UTC


README

Rather than have a large PHP array of countries in your code, this package aims to help speed up time and tidy your project by returning all countries and the data you need in a sensible format, following the ISO-3166-1 standard.

This makes sense where you may need to provide a <select> HTML element of countries or seed your database with constant data, for example.

Installation

composer require liamjcooper/php-countries

Usage

Return all countries

<?php

use LiamJCooper\Countries\Builder;

...

(new Builder)->all();

//  returns: [
//      ...
//      ['name' => 'United Kingdom', 'iso_2' => 'GB', 'iso_3' => 'GBR', 'code' => '826'],
//      ['name' => 'United States', 'iso_2' => 'US', 'iso_3' => 'USA', 'code' => '840']
//      ...
//  ]

Search by value

<?php

use LiamJCooper\Countries\Builder;

...

(new Builder)->where('iso_2', 'GB');

//  returns: [
//      ['name' => 'United Kingdom', 'iso_2' => 'GB', 'iso_3' => 'GBR', 'code' => '826']
//  ]