dpn517111 2016-06-09 13:08
浏览 46
已采纳

在没有密钥的PHP中排序多维数组? [重复]

This question already has an answer here:

I want to sort a multidimensional array in numerical order bu second value of each item.

This is array:

$vulc = array(
        array('s',3),
        array('s',5),
        array('s',2)
    );

and i want this output:

$vulc = array(
        array('s',5),
        array('s',3),           
        array('s',2)
    );

I'm tried this:

foreach ($vulc as $key => $row) {
    $distance[$key] = $row[2];
}

array_multisort($distance, SORT_ASC, $vulc);

but doesn't work :( please help me and not suggest to see other answer... usually other answer are too difficult :(

</div>
  • 写回答

3条回答 默认 最新

  • dqoag62688 2016-06-09 13:11
    关注

    Use the usort function and Try:

    function sortByOrder($a, $b) {
        return $b[1] - $a[1]; // $b[1] - $a[1] because it need to be descending order
    }
    
    usort($vulc, 'sortByOrder');
    

    Output:

    Array
    (
        [0] => Array
            (
                [0] => s
                [1] => 5
            )
    
        [1] => Array
            (
                [0] => s
                [1] => 3
            )
    
        [2] => Array
            (
                [0] => s
                [1] => 2
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

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