PHP uksort() Function


Complete PHP Array Reference

Definition and Usage

The uksort() function sorts an array by the element keys using user defined comparison function.

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

This function is useful for sorting with custom algorithms.

Syntax

uksort(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;
  }

$people = array("Swanson" => "Yousuf",
"Dharsan" => "William", "Boopathi" => "Glenn",
"swanson" => "Yousuf", "Dharsan" => "William",
"Boopathi" => "glenn");

uksort($people, "my_sort");

print_r ($people);
?>

The output of the code above will be:

Array
(
[swanson] => Yousuf
[Boopathi] => glenn
[Dharsan] => William
[Swanson] => Yousuf
[Boopathi] => Glenn
[Dharsan] => William
)


Complete PHP Array Reference
Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!