Sort 2D Array in PHP

multidimensional-array, php, sorting

Solution

Use `ksort` (or `uksort`) to sort the arrays by their keys.

UPDATE: Use `asort` (or `uasort`) to sort by values, preserving keys.

UPDATE 2: Try this

foreach($distance as &$value) {
    asort($value,SORT_NUMERIC);
}

Problem

I have an array that looks like this: ``` Array ( [90] => Array ( [1056] => 44.91 [1055] => 53.56 [1054] => 108.88 [1053] => 23.28 ), [63] => Array ( [1056] => 44.44 [1055] => 53.16 [1054] => 108.05 ), [21] => Array ( [1056] => 42.83 [1055] => 51.36 [1054] => 108.53 ) ); ``` Both keys ([x] and [y]) refer to IDs in my database, so those need to stay intact. The order of the [x] does not matter, but I need to sort each array by the value of [y]. Edit: I have tried this loop, but it does not seem to work: ``` foreach($distance as $key=>$value) { asort($value,SORT_NUMERIC); } ```

Original source