PHP uasort() Function
Complete PHP Array Reference
Definition and Usage
The uasort() function sorts an array by a user defined comparison function.
This function does not assigns new keys for the elements in the array.
This function returns TRUE on success, or FALSE on
failure.
This function is useful for sorting with custom algorithms.
Syntax
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" => "Joe",
"Griffin" => "Peter", "Quagmire" => "Glenn",
"swanson" => "joe", "griffin" => "peter",
"quagmire" => "glenn");
uasort($people, "my_sort");
print_r ($people);
?>
|
The output of the code above will be:
Array
(
[griffin] => peter
[swanson] => joe
[quagmire] => glenn
[Griffin] => Peter
[Swanson] => Joe
[Quagmire] => Glenn
)
|
Complete PHP Array Reference
|
|
|
See why there are 20,000+ Ektron integrations worldwide.
Request an INSTANT DEMO or download a FREE TRIAL today. |
|
|
|