Web | This Site

Learning PHP
Advanced PHP
PHP Function Reference
PHP Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# PHP Function uksort()

## Syntax

uksort ( $array, $cmp_function )

## Definition and Usage

The uksort() function sorts an array by the element keys using user defined comparison function.

## Paramters

Parameter | Description
--- | ---
array | Required. Specifies an array.
cmp_function | Required. Usef defined function to compare values and to sort them. The function must return -1, 0, or 1 for this method to work correctly. It should be written to accept two parameters to compare, and it should work something like this:

- If a = b, return 0
- If a > b, return 1
- If a < b, return -1

## Return Value

Returns TRUE on success or FALSE on failure.

## Example

Try out following example:

    <?php
    function cmp_function($a, $b)
    {
      if ($a == $b) return 0;
      return ($a > $b) ? -1 : 1;
    }

    $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana" );
    uksort($fruits, "cmp_function");
    print_r($fruits);
    ?>

This will produce following result:

    Array ( [d] => lemon [b] => banana [a] => orange )

Advertisement

Online Image Processing

Indian Baby Names