From http://www.w3schools.com (Copyright Refsnes Data)

PHP usort() Function


PHP Array Reference Complete PHP Array Reference

Definition and Usage

The usort() function sorts an array by a user defined comparison function.

This function assigns new keys for the elements in the array. Existing keys will be removed.

This function returns TRUE on success, or FALSE on failure.

This function is useful for sorting with custom algorithms.

Syntax

usort(array,sorttype)

Parameter Description
array Required. Specifies the array to sort
function Required. A user specified function.

The function must return -1, 0, or 1 for this method to work correctly. It should be written to accept two parameters to compare, and it should work something like this:

  • If a = b, return 0
  • If a > b, return 1
  • If a < b, return -1


Example

<?php
function my_sort($a, $b)
  {
  if ($a == $b) return 0;
  return ($a > $b) ? -1 : 1;
  }

$arr = array("Peter", "glenn","Cleveland",
"peter","cleveland", "Glenn");
usort($arr, "my_sort");

print_r ($arr);
?>

The output of the code above will be:

Array
(
[0] => peter
[1] => glenn
[2] => cleveland
[3] => Peter
[4] => Glenn
[5] => Cleveland
) 


PHP Array Reference Complete PHP Array Reference

From http://www.w3schools.com (Copyright Refsnes Data)