0

I have an array like this in my scenario

$arr[0]['id']=1200;
$arr[0]['rate']=7.0;
$arr[1]['id']=1380;
$arr[1]['rate']=7.0;
$arr[2]['id']=1568;
$arr[2]['rate']=7.0;
$arr[3]['id']=1933;
$arr[3]['rate']=7.0;

when I sorted it using the usort as below, it reversed the order

usort($arr,function($a, $b) {
        return $b['rate'] < $a['rate'];
});


[1933,1568,1380,1200]

but I want it to remain in the same order as it is inputted, if the rate is same, as represented below

[1200,1380,1568,1933] 

Please help, thanks in advance

user3357227
  • 139
  • 1
  • 2
  • 10
  • Apologies - I was about to delete my post when you commented. Unfortunately the behaviour of usort changed way back in 4.1, such that what you're asking for isn't possible using that function. There are a lot of solutions for what you're asking in the [comments in the manual](http://php.net/usort). Search for "equal" and you'll find them – Kelvin Mar 18 '14 at 19:14
  • See http://stackoverflow.com/questions/4353739/preserve-key-order-stable-sort-when-sorting-with-phps-uasort for a ready to use solution – Kelvin Mar 18 '14 at 19:17

0 Answers0