Created
September 22, 2021 05:57
-
-
Save Bodom78/4333fca67c5f306eb6a44f0f8fa696c5 to your computer and use it in GitHub Desktop.
Create a PHP array of Country Codes and Names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Will write/overwrite an array to countries_en.php in the same directory. | |
* $countries = [ | |
* "AW" => "Aruba", | |
* "AF" => "Islamic Republic of Afghanistan", | |
* "AO" => "Republic of Angola", | |
* "AI" => "Anguilla", | |
* ... | |
* @see https://restcountries.com for more information. | |
*/ | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, 'https://restcountries.com/v3/all'); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
$result = curl_exec($curl); | |
curl_close($curl); | |
$countries = json_decode($result); | |
$filename = 'countries_en.php'; | |
file_put_contents($filename, | |
"<?php\n\n\$countries = [\n"); | |
foreach ($countries as $country) { | |
$country_code = $country->cca2; | |
$country_name = $country->name->official; | |
file_put_contents($filename, | |
"\t".'"'.$country_code.'" => "'.$country_name.'",'.PHP_EOL, FILE_APPEND); | |
} | |
file_put_contents($filename, "];", FILE_APPEND); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment