1. Go to this page and download the library: Download stefangabos/world_countries library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// this function assumes that you have done this:
$countries = rch yields a result
// returns false if no results could be found or if argument is incorrect
function search_country($query) {
// make the countries available in the function
global $countries;
// if argument is not valid return false
if (!isset($query['id']) && !isset($query['alpha2']) && !isset($query['alpha3'])) return false;
// iterate over the array of countries
$result = array_filter($countries, function($country) use ($query) {
// return country's data if
return (
// we are searching by ID and we have a match
(isset($query['id']) && $country['id'] == $query['id'])
// or we are searching by alpha2 and we have a match
|| (isset($query['alpha2']) && $country['alpha2'] == strtolower($query['alpha2']))
// or we are searching by alpha3 and we have a match
|| (isset($query['alpha3']) && $country['alpha3'] == strtolower($query['alpha3']))
);
});
// since "array_filter" returns an array we use pop to get just the data object
// we return false if a result was not found
return empty($result) ? false : array_pop($result);
}