I am new to Laravel and I was wondering if I can order the following multidimensional array with countries alphabetically? So I want all the countries inside the continents to be ordered alphabetically.
"EU" => array:9
0 => "NL"
1 => "BE"
3 => "FR"
4 => "DE"
5 => "ES"
6 => "IT"
7 => "GB"
8 => "TR"
9 => "DK"
]
"AS" => array:2
]
CodePudding user response:
It seems you need to sort an inner array by values. Here is a one way to do it.
$arr = [
"EU" => [
0 => "NL",
1 => "BE",
3 => "FR",
4 => "DE",
5 => "ES",
6 => "IT",
7 => "GB",
8 => "TR",
9 => "DK",
],
"AS" => ["AB", "AA"]
];
foreach($arr as $continent => $countries) {
asort($countries);
$arr[$continent] = $countries;
}
var_dump($arr);
CodePudding user response:
Laravel has nothing to do with these stuff. You should use PHP functions and here's an example:
$arr = [
0 => "NL",
1 => "BE",
3 => "FR",
4 => "DE",
5 => "ES",
6 => "IT",
7 => "GB",
8 => "TR",
9 => "DK",
];
sort($arr);
print_r($arr);
The result:
Array
(
[0] => BE
[1] => DE
[2] => DK
[3] => ES
[4] => FR
[5] => GB
[6] => IT
[7] => NL
[8] => TR
)