I have read about uksort in the PHP manual but it is very difficult to understand.
Can any one help me out?
Here is an example:
<?php
function my_sort($x, $y)
{
if ($x == $y) return 0;
return ($x > $y) ? -1 : 1;
}
$people = array(
"10" => "javascript",
"20" => "php", "60" => "vbscript",
"40" => "jsp");
uksort($people, "my_sort");
print_r($people);
?>
What is happening here?
As said in the manual, your function (
my_sortin this case) should return:-1) if you consider$ato be less than$b$ato be greater than$b0if you consider them to be the same.As you may have guessed,
uksortwill use your comparison function to see in which order the elements should be in the sorted array. It will call your function multiple times, every time with two keys. You compare those to keys to each other and give your result back.The idea is that you can program your own comparison function which does something non-trivial, for example if you want a certain key to always be first. Your trivial example can use the regular krsort instead.