-1

I have an array

$DATA = array(
array(
    "id" => "23",
    "rate" => "4.555"
),
array(
    "id" => "12",
    "rate" => "4.555"
),
array(
    "id" => "20",
    "rate" => "4.555"
),

array(
    "id" => "29",
    "rate" => 5.1025"
)   
);

Now i need to sort above array by key: rate (ascending) and id (ascending).

So:

    function mySort($a, $b) {

      return strcmp($a['rate'], $b['rate']); 

    } 

uasort($DATA,'mySort');

Now sort perfect but only by rate....

Adding new funcion:

function mysortID ($a,$b){ //AD
        return ($a['id'] > $b['id']) ? 1 : -1;  
    }

Let's try:

 uasort($DATA,'mySort');
 uasort($DATA,'mySortID');

But doesn't work.... how to that ?

jasne
  • 53
  • 1
  • 11
  • This question can now be deleted so it does not stand in the way of the many other duplicates that much. – hakre Oct 21 '13 at 12:59

1 Answers1

4
function mySort($a, $b)
{
    // Check the rates
    $res = strcmp($a['rate'], $b['rate']);

    // If the rates are the same...
    if ($res === 0) {
        // Then compare by id
        $res = $a['id'] > $b['id'] ? 1 : -1;
    }

    return $res;
}
Sean Bright
  • 118,630
  • 17
  • 138
  • 146