Array sort

hi everybody , I would appreciate gentlemanly who help me …
I’ve an array within structure below :

Array
(
    [0] => Array
        (
            [id] => 1
            [username] => admin
            [password] => 38ed5d0690b3c864cbaff2f599d8d9b3
            [total] => 1582
        )

    [1] => Array
        (
            [id] => 2
            [username] => name
            [password] => f2f599d8d9b338ed5d0690b3c864cbaf
            [total] => 529
        )
    [2] => Array
        (
            [id] => 3
            [username] => yed
            [password] => 4cbaff2f5d0690b3c86599d8d9b338ed
            [total] => 50000
        )
       .............
       .............
       .............
       .............
)

I’ve integer value in [total] in each array , I just wanna sort the array by the highest [total] value to lowest …
for example :

Array
(
    [0] => Array
        (
            [id] => 3
            [username] => yed
            [password] => 4cbaff2f5d0690b3c86599d8d9b338ed
            [total] => 50000
        )

    [1] => Array
        (
            [id] => 1
            [username] => admin
            [password] => 38ed5d0690b3c864cbaff2f599d8d9b3
            [total] => 1582
        )
    [2] => Array
        (
            [id] => 2
            [username] => name
            [password] => f2f599d8d9b338ed5d0690b3c864cbaf
            [total] => 529
        )
       .............
       .............
       .............
       .............
)

sry for y bad English I wish I could tell you my meaning :]

You could use [fphp]usort[/fphp], but if this data is coming from a database you should use ORDER BY total in your query.


<?php
function sortByValue(array $a, array $b){
  if($a['total'] === $b['total']){
    return 0;
  }
  return $a['total'] > $b['total'] ? 1 : -1 ;
}

$values = array(
  array('total' => 1210, 'id' => 3),
  array('total' => 1205, 'id' => 2),
  array('total' => 1215, 'id' => 4),
  array('total' => 1200, 'id' => 1),
);

usort($values, 'sortByValue');

print_r(
  $values
);

/*
  Array
  (
    [0] => Array
    (
      [total] => 1200
      [id] => 1
    )
    [1] => Array
    (
      [total] => 1205
      [id] => 2
    )
    [2] => Array
    (
      [total] => 1210
      [id] => 3
    )
    [3] => Array
    (
      [total] => 1215
      [id] => 4
    )
  )
*/

I’m not using database , thank you very much :X