开发者

How to sort an array in array by a specific key in PHP [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Sorting an associative array in PHP

How to sort a multidimensional array by a certain key?

I would like to know how can I sort an array in array by a specific key.

For example:

$array = array( 0 => array("id"=>25), 1 => array("id"=>15) , 2 => array("id"=>19) );

now I want to sort the array by the key "id", 开发者_Python百科i'm expecting this result:

$array = array( 0 => array("id"=>15), 1 => array("id"=>19) , 2 => array("id"=>25) );

Anybody can help ?

Thanks


Hope this may work.

function cmp($a, $b){
    return $a['id'] - $b['id'];
}
usort($array, 'cmp');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜