0

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
]
Elisaveta
  • 39
  • 5
  • Related: [Sort data in column of a multi-dimensional array in a descending direction while preserving keys](https://stackoverflow.com/q/74635226/2943403) – mickmackusa Dec 01 '22 at 21:25

4 Answers4

0

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);
Nairi Abgaryan
  • 616
  • 5
  • 16
0

Since you are looking for a Laravel answer, you might be interested in the Arr::sortRecursive() helper for a short and fluent implementation. Let me demonstrate the code for your data while also expending your data a bit more to the my point across.

use Illuminate\Support\Arr;

$array = [
  "EU" => [
    "NL", "BE", "FR", "DE", "ES", "IT", "GB", "TR", "DK",
  ],
  "AS" => [
    "JA", "IN", "CH",
  ],
  "NA" => [
    "US", "ME", "CA",
  ],
];
 
$sorted = Arr::sortRecursive($array);

/*
[
  "AS" => [
    "CH", "IN", "JA",
  ],
  "EU" => [
    "BE", "DE", "DK", "ES", "FR", "GB", "IT", "NL", "TR",
  ],
  "NA" => [
    "CA", "ME", "US",
  ],
]
*/
Robin Bastiaan
  • 572
  • 2
  • 8
  • 21
0

Your input array is an associative array of associative arrays (has a consistent depth of 2 levels) therefore recursion is not necessary.

Use a classic loop or functional iterator (like array_walk()), modify the rows by reference and call sort(). Judging by your comment under another answer, you need to call array_unique() on each row as well -- to remove duplicate values.

Code: (Demo)

foreach ($arr as &$row) {
    sort($row);
}
var_export($arr);

For functional-style programming, array_walk() can sort the rows concisely. I've left out the duplicate removal call. (Demo)

array_walk($arr, fn(&$row) => sort($row));
var_export($arr);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • (Surely this is a duplicate question, but I couldn't find one ...and I even did a SEDE search. I must be using the wrong keywords.) – mickmackusa Dec 01 '22 at 22:36
-1

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
)
WebPajooh
  • 442
  • 1
  • 3
  • 12