Javatpoint Logo
Javatpoint Logo

PHP uksort() function

The PHP uksort( ) function is used to sort an array by the elements keys using a user-defined comparison 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 uksort( ) function returns true on success or false on failure.

Example 1

Output:

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

Example 2

Output:

Array
(
    [6] => python
    [4] => ruby
    [2] => php
    [1] => java
)

Example 3

Output:

Array
(
    [a] => 9
    [b] => 2
    [c] => 60
    [d] => 78
)

Example 4

Output:

Array
(
    [8] => football
    [4] => badminton
    [2] => hockey
    [1] => cricket
)





Help Others, Please Share

facebook twitter pinterest