Javatpoint Logo
Javatpoint Logo

PHP usort( ) function

The PHP usort( ) function is used to sort an array by its value using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function. This function was introduced in PHP 4.0.

Syntax

Parameter

Parameter Description Is compulsory
array Specifies the array to be sorted. compulsory
user_defined_function This 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 usort( ) function returns true on success and false on failure.

Example 1

Output:

Array
(
    [0] => denmark
    [1] => bangladesh
    [2] => australia
)

Example 2

Output:

Array
(
    [0] => 8
    [1] => 11
    [2] => 56
    [3] => 96
)

Example 3

Output:

Array
(
    [0] => c++
    [1] => csharp
    [2] => java
    [3] => php
)

Example 4

Output:

Array
(
    [0] => aajtak
    [1] => abp
    [2] => ndtv
    [3] => zee news
)





Help Others, Please Share

facebook twitter pinterest