I’ve stored countries with their codes in an array like below
$countries = array (
'AF' => 'Afghanistan',
'AX' => 'Åland Islands',
'AL' => 'Albania',
'DZ' => 'Algeria'
);
Suppose I got the country code ‘AF’ or country name ‘Afghanistan’ from a form, how do I output both the code and name from the array?
The output I’m trying to get is: Afghanistan AF. I’ll later use that in code below.
echo $countries['AF'] gives me the country name, but how do I output both;
If you know the code (‘AF’ in this case
But if you want to got the other way i.e. Afghanistan to get the code AF, you have to iterate throught the list or construct another array.