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] );
}
精彩评论