drgc9632 2014-10-20 22:14
浏览 54
已采纳

按整数值排序多维数组

I have the following array:

Array
(
[Address Line 1] => Array
    (
        [llx] => 18.96
        [lly] => 28.88999999999999
    )

[Address Line 2] => Array
    (
        [llx] => 18.73
        [lly] => 35.66
    )

[City State ZIP Code] => Array
    (
        [llx] => 18.51
        [lly] => 47.16999999999999
    )

[Full Name] => Array
    (
        [llx] => 18.86
        [lly] => 20.15999999999997
    )

 )

and i would like to loop through it and sort it by the lly value: so the final outcome should really be

Array
 (
[Full Name] => Array
    (
        [llx] => 18.86
        [lly] => 20.15999999999997
    )

[Address Line 1] => Array
    (
        [llx] => 18.96
        [lly] => 28.88999999999999
    )

[Address Line 2] => Array
    (
        [llx] => 18.73
        [lly] => 35.66
    )

[City State ZIP Code] => Array
    (
        [llx] => 18.51
        [lly] => 47.16999999999999
    )

  )

Any ideas on accomplishing this? Iv been looking over the sorting functions in php but they seem to be mainly focused on single dimension array and nothing like this unless im missing something

  • 写回答

2条回答 默认 最新

  • donglang5157 2014-10-20 22:28
    关注

    Rudie already has the best answer for your particular case, but here's an alternative using array_multisort

    $llx = array();
    $lly = array();
    foreach ($array as $key => $row) {
        $llx[$key] = $row['llx'];
        $lly[$key] = $row['lly'];
    };
    array_multisort($lly, SORT_ASC, $llx, $array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?