doumiang0597 2010-01-19 13:53
浏览 50
已采纳

根据PHP中的值对数组进行排序

I have the following array

[0] => Array
    (
        [id] => 229
        [val] => 2
    )

[3] => Array
    (
        [id] => 237
        [val] => 1
    )

[4] => Array
    (
        [id] => 238
        [val] => 6
    )

I need to sort this array according to the val values in the array, and do not know how to accomplish this?

  • 写回答

5条回答 默认 最新

  • duandaodao6951 2010-01-19 13:56
    关注
    function cmp($a, $b)
    {
        if ($a["val"] == $b["val"]) {
            return 0;
        }
        return ($a["val"] < $b["val"]) ? -1 : 1;
    }
    
    usort($yourarray, "cmp");
    

    Read this for more information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?