» tagged pages
» logout
User:alex
Return to Tips and Tricks

PHP multi-sort

Make further edits, (or Cancel)

(or Cancel)

(Editing anonymously: to be credited for your changes, login or register a new account)

(or Cancel)

Tags Applied to this Entry

1 person has tagged this page:

I like this function in PHP:


function multi_sort($array, $akey)
{
  function compare($a, $b)
  {  
     global $key;
     if ($a[$key]>$b[$key]){
         $varcmp = "1";
         return $varcmp;
     }
     elseif ($a[$key]<$b[$key]){
         $varcmp = "-1";
         return $varcmp;
     }
     elseif ($a[$key]==$b[$key]){
         $varcmp = "0";
         return $varcmp;
     }
  }
  usort($array, "compare");
  return $array;
}

You call this function like this:
$array = multi_sort($array, $key = ‘keyName’);

this will set the array to a sorted version sorted by key ascending

Username:
Password:
(or Cancel)