开发者

Sorting an array based on its value

I have an Array, Sample:

$array {
 [0] {
   [something]=1;
   [something2]=2;
     } 
 [1] {
   [something]=2;
   [something2]=4;
     }
 [2] {
   [something]=5;
   [something2]=2;
     }
}

I want to ord开发者_如何学Pythoner the array based on the key something;

So it will look like:

$array {
 [0] {
   [something]=5;
   [something2]=2;
     } 
 [1] {
   [something]=2;
   [something2]=4;
     }
 [2] {
   [something]=1;
   [something2]=2;
     }
}


function compare($x, $y) {
    return $x['something'] - $y['something'];
}

usort($input_array, 'compare');

you need to use a usort() similar to the above.


Would the following suffice?

foreach($array as $key => $value){
    ksort( $array[$key] );
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜