Sorting a 2D array in PHP

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]) relate to identifiers in my database, so they should remain intact. The order [x] does not matter, but I need to sort each array by the value [y].

Edit: I tried this loop, but it does not work:

foreach($distance as $key=>$value) {
    asort($value,SORT_NUMERIC);
}
+3
source share
4 answers

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

UPDATE: use asort(or uasort) to sort by values, keeping keys.

UPDATE 2: Try this

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

Like this?

array_walk($array, 'asort');
+4
source

asort() . .

$value , &$value.

+2
 array_multisort($arrindex1, SORT_DESC, $arrindex2, SORT_DESC, $array);
0

Source: https://habr.com/ru/post/1779035/


All Articles