PHP uasort() Function

6 Jan 2025 | 2 min read

The PHP uasort( ) function is used to sort an array by its values using a user-defined comparison function. This function was introduced in PHP 4.0.

Syntax

Parameter

ParameterDescriptionIs compulsory
arraySpecifies the array to be sorted.compulsory
user_defined_functionThis function is a comparison function and it is used to compare values and sort the array. This function returns three types of values.
  1. If a=b, return 0
  2. If a>b, return 1
  3. If a<b, return -1
Optional

Return type

The uasort( ) function returns true on success or false on failure.

Example 1

Output:

 Array ( [b] => badminton [d] => cricket [c] => football [a] => tennis ) 

Example 2

Output:

 Array ( [2] => tata [4] => ford [1] => datsun [5] => audi ) 

Example 3

Output:

 Array ( [d] => coconut [b] => banana [a] => apple ) 

Example 4

Output:

 Array ( [f] => 4 [b] => 3 [a] => 2 [d] => 1 ) 

Next Topic