English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP uksort() Function Usage and Example

PHP Array Function Manual

 The uksort() function sorts the keys in the array using a user-defined comparison function

Syntax

uksort ( $array, $cmp_function )

Definition and Usage

The uksort() function sorts the array by element key using a user-defined comparison function.

Parameter

Serial NumberParameters and Description
1

array(Required)

It specifies an array.

2

cmp_function(Required)

If the function is defined, it is used to compare the values and sort them.

The function must return-1、0 or1,this method can work normally. It should be written to accept two parameters for comparison, and its working method should be similar to the following−

  • If a = b, then return 0

  • If a > b, then return1

  • If a < b, then return-1

Return Value

Returns TRUE on success, FALSE on failure.

Online Example

<?php
   function cmp_function($a, $b) {
      if ($a == $b) return 0;
      return ($a > $b) ? -1 : 1;
   }
   
   $input = array("d"=>"lemon", "a"=>"orange", "b"=>"banana" );
   uksort($input, "cmp_function");
   
   print_r($input);
?>
Test and see‹/›

Output Result:

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

  PHP Array Function Manual