douhao2856 2010-12-07 19:39
浏览 32
已采纳

在PHP中排序2D数组

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);
}
  • 写回答

4条回答 默认 最新

  • doubiao7410 2010-12-07 19:42
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?