How to use uksort in PHP
In this article, I will explain how the uksort() function can be used in PHP.
uksort() function in PHP
- The uksort() function is used to sort an array by key using a user-made comparison function.
- The uksort() function does not assigns new keys for the elements.
- The uksort() function is useful for sorting with custom algorithms.
- The uksort() function maintains the existing key index.
- The uksort() function returns TRUE on success, or FALSE on failure.
Syntax
Parameter
- array array is required parameter. the determine array to be sort.
-
function function is required parameter. the function must return -1, 0, or 1 for this method to work correctly. It should be written to accept two parameters to compare.
- If a = b, return 0
- If a > b, return 1
- If a < b, return -1
Example
The following example show to how the uksort() function can be used in PHP.
<html>
<body>
<h3 style="color: violet;">uksort() function example in PHP</h3>
<?php
function my_sortdemo($x, $y)
{
if ($x == $y) return 0;
return ($x > $y) ? -1 : 1;
}
$language_list = array("5" => "JavaScript".'<br/>',
"10" => "Linux".'<br/>', "50" => "Asp.net".'<br/>',
"30" => "C#".'<br/>');
uksort($language_list, "my_sortdemo");
print_r ($language_list);
?>
</body>
</html>
|
Output
You may also want to read these related articles here
Got a programming related question? You may want to post your question here