PHP uasort() function

PHP sort() function
PHP uksort() function

In this article, you will learn how to sort an array using its values based on some user-defined function. The uasort() function takes values from the array and passes them to the user-defined function and sorts them according to it.

What is the syntax of the uasort() function in PHP?

uasort(array, myfunction)
ParameterDetails
arrayThe array to sort – Required
my_functionCallable function name (string).
The callable function compares two values and returns an integer <, =, or > 0 if the first value is <, = or > than second.
PHP uasort() function

Examples of uasrt() function

Example 1. Sort the array elements by values using the user-defined function that compares values.

<?php
function my_function($x,$y)
{
if ($x==$y) return 0;
return ($x<$y)?-1:1;
}

$arr=array("alpha"=>1,"beta"=>3,"gama"=>8,"delta"=>0);
uasort($arr,"my_function");
?>

PHP sort() function
PHP uksort() function
en English
X
Scroll to Top