开发者

Sort 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]) 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:开发者_JAVA技巧

foreach($distance as $key=>$value) {
    asort($value,SORT_NUMERIC);
}


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);
}


Like this?

array_walk($array, 'asort');


Use asort() for sorting by values. It maintains the index associations.

For the loop, you need to pass $value by reference, so you need to use &$value.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜