dongnue2071 2016-09-29 18:32
浏览 37
已采纳

在php中排序多个索引数组

how to sort this kind of array? this will be sort by array[x][1]. can this be sorted using usort?

Array
(
    [0] => Array
        (
            [0] => 1247
            [1] => 3
            [2] => no
            [3] => no
        )

    [1] => Array
        (
            [0] => 224
            [1] => 1
            [2] => no
            [3] => no
        )

    [2] => Array
        (
            [0] => 226
            [1] => 2
            [2] => no
            [3] => no
        )

)
  • 写回答

1条回答 默认 最新

  • douzhong3038 2016-09-29 18:47
    关注

    You're on the right track with usort(), you simply need to compare against the 2nd element in the array (as you required).

    usort($array, function($i, $v) {
        return $i[1] - $v[1];
    });
    

    Note: The above $array is your array that you want to sort.

    Which will return it in the correct order (1,2 then 3).

    Example/Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部