I have a JSON array like this
$countries = [
{
"id": "1",
"country_name": "Afghanistan",
},
{
"id": "2",
"country_name": "Albania",
},
{
"id": "3",
"country_name": "Algeria",
},
{
"id": "4",
"country_name": "American Samoa",
}
..
..
..
{
"id": "50",
"country_name": "Zimbabwe",
}
];
The following array contains, the list of countries that I need to sort to top
$top_countries = ['United States', 'United Kingdom', 'German'];
What is the best way to sort the above array as follows
$countries = [
{
"id": "30",
"country_name": "United States",
},
{
"id": "31",
"country_name": "United Kingdom",
},
{
"id": "20",
"country_name": "German",
},
{
"id": "1",
"country_name": "Afghanistan",
},
{
"id": "2",
"country_name": "Albania",
},
{
"id": "3",
"country_name": "Algeria",
},
{
"id": "4",
"country_name": "American Samoa",
}
..
..
..
{
"id": "50",
"country_name": "Zimbabwe",
}
];
json_decode()+uasort()+json_encode()