I hope you are doing well.
I want to get all the countries in the below format.
$countries = [
"AU" => "Australia",
"IN" => "India",
"US" => "United States",
"UK" => "United Kingdom",
.......
]
I tried but no use, i am always getting CountryId as 2 digit names like
$countries = [
0 => "AU",
1 => "IN",
2 => "US",
3 => "UK",
.......
]
I have used the following code
MagentoDirectoryModelResourceModelCountryCollection::loadByStore()
How can i achieve this in Magento 2 ?
Thanks in advance.
2
Answers
I think you need to loop through the country collection and create a new array with the country code as key and the country name as value. Could you try something like this?
Then in your template you can get the countries with $block->getCountries().
Result:
Use my simple code: (can ez injection class)