uksort() function in PHP


The uksort() function sorts an array by keys using a user-defined function. It returns TRUE on success and FALSE on failure.

Syntax

uksort(arr, custom_function)

Parameters

  • arr − The specified array.

  • custom_function − The comparison function. It must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Return

The uksort() function returns TRUE on success and FALSE on failure.

Example

The following is an example −

 Live Demo

<?php
function display($x,$y) {
   if ($x==$y) return 0;
   return ($x<$y)?-1:1;
}
$myarr = array("a"=>99,"b"=>27,"c"=>56);
uksort($myarr,"display");

foreach($myarr as $x=>$x_value) {
   echo "Key=" . $x . " Value=" . $x_value;
   echo "<br>";
}
?>

Output

Key=a Value=99
Key=b Value=27
Key=c Value=56

Updated on: 24-Jun-2020

31 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements