PHP uksort() Function

PHP uksort() Function

The uksort() function in PHP sorts an array by keys using a user-defined comparison function.

Syntax

uksort ( array &$array , callable $key_compare_func )

Parameter

array(required)- This parameter represents the input array.

key_compare_func(required)- This parameter represents a string that describe a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.

Return

This function returns a Boolean value TRUE on success or FALSE on failure.

Example 1

"Reema",2=>"Varun",1=>"Joe",4=>"Ron",3=>"Jai");
 //original array
 echo("Original array: \n");
 print_r($array);
 //sorting the array by keys using "strcasecmp" comparison function
 uksort($array,"strcasecmp");
 //printing the sorted array
 echo("\nSorted array: \n"); 
 print_r($array);
 ?> 

Output

Original array: 
 Array
 (
     [5] => Reema
     [2] => Varun
     [1] => Joe
     [4] => Ron
     [3] => Jai
 ) 
 Sorted array: 
 Array
 (
     [1] => Joe
     [2] => Varun
     [3] => Jai
     [4] => Ron
     [5] => Reema
 ) 

Example 2

$b)? 1: 0;  
 } 
 // initializing the Input Array
 $array1 = array(15=>"Reema", 10=>"raj", 30=>"Atul"); 
 echo("Original Array: \n");
 print_r($array1);
 // Sorting an array by keys using "my_function" comparison function
 uksort ($array1,"my_function");
 echo("\nSorted Array: \n"); 
 print_r($array1);
 ?> 

Output

Original Array: 
 Array
 (
     [15] => Reema
     [10] => raj
     [30] => Atul
 )
 Sorted Array:  
 Array
 (
     [10] => raj
     [15] => Reema
     [30] => Atul
 ) 

Example 3

"Reema", "Age"=>23,"Country"=>"India");
 //original array
 echo("Original array: \n");
 print_r($array);
 //sorting the array with keys
 uksort($array,"strcasecmp" );
 //printing the sorted array
 echo("\nSorted array: \n");
 print_r($array);
 ?> 

Output

Original array: 
 Array
 (
     [Name] => Reema
     [Age] => 23
     [Country] => India
 )
 Sorted array without second parameter: 
 Array 
 (
     [Age] => 23
     [Country] => India
     [Name] => Reema
 ) 

Related Topics

PHP array_combine() Function

PHP array_combine() Function The array_combine() function in PHP is used to create an array by combining two arrays i.e., one array for keys and another array for its values. Syntax array_combine ( array $keys , array $values ) Parameter keys(required)- This parameter represents an array which acts...

1 minute read.

PHP array_diff() Function

The array_diff () function in PHP compares array1 against other specified arrays and returns the values in array1 that are not present in any of the other arrays. Syntax array_diff(array$array1,array$array2[,array$...] ) Parameter array1(required)- This parameter signifies the input array to...

1 minute read.

PHP trim() Function

PHP trim() Function The trim() Function in PHP strips the whitespace or other characters from the beginning and the end of the given string. The trim() function strips the following characters: " " - an ordinary space."\t"- a tab."\n"- a new...

1 minute read.

PHP wordwrap() Function

PHP wordwrap() Function The wordwrap() Function wraps a string to a given number of characters using a string break character. Syntax wordwrap ( string $str [, int $width [, string $break  [, bool $cut  ]]] ) Parameter str(required)- This parameter specifies the input string.       width(optional)- This parameter represents the number of...

1 minute read.

PHP Continue

What is continue in PHP? Inside the loop, the continue statement is used to control the loop in preprocessor hypertext. PHP utilizes the continue keyword to implement the continue statement, which...

3 minutes read.

PHP array_walk() Function

PHP array_walk() Function The array_walk() function in PHP is used to apply the user-defined callback function to each element of the array. This function returns a Boolean TRUE on success or FALSE on failure. Syntax array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] ) Parameter array(required)- This array represents...

1 minute read.

PHP ucfirst() Function

PHP ucfirst() Function The ucfirst() function in PHP converts the first character of the specified string to uppercase. Syntax ucfirst ( string $str ) Parameter str(required)- This parameter represents the input string. Return This function returns a string with the first character of the specified string capitalized if that...

1 minute read.

PHP Associative Arrays

PHP associate array is used to access the elements with keys by “=>” symbol. There are two ways to create an associative array. 1st way: $age = array("John"=>"20", "Helena"=>"30", "Rasmus"=>"29"); 2nd way: $age['john'] = "20"; $age['Helena'] =...

1 minute read.

PHP sscanf() Function

PHP sscanf() Function The sscanf() function in PHP parses input from a string according to a given format. Syntax sscanf ( string $str , string $format [,mixed &$arg1,$agr2,$arg++... ] ) Parameter str(required)- This parameter specifies the string to read. format(required)- This parameter specifies the format to use. The conversion specification...

3 minutes read.

PHP is_iterable() Function

The is_iterable() function in PHP verifies that the contents of the specified variable is an iterable value. Syntax is_iterable ( mixed $var ) var(required)- This parameter represents the value to check. Return This function returns a Boolean value TRUE if the parameter var is iterable, else it returns...

1 minute read.

PHP usort() Function

PHP usort() Function The usort() function in PHP sorts  array by values using a user-defined comparison function. Syntax usort ( array &$array , callable $value_compare_func ) array(required)- This parameter represents the input array. value_compare_func(required)- This parameter represents a string that describe a callable comparison function. The comparison...

1 minute read.

PHP if..Else..Elseif

PHP conditional statements are used to test the conditions. It supports different types of conditional statement. Following are the types of conditional statement if statement ..else statement ..elseif....else statement switch statement PHP if Statement PHP...

2 minutes read.

PHP array_reduce() Function

PHP array_reduce() Function The array_reduce() function in PHP reduces the array to a single value using by applying a callback function to the elements of the array. Syntax array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] ) Parameter array(required)- This parameter represents the input array. callback (required)- This...

1 minute read.

PHP echo() Function

PHP echo() Function The echo() function in PHP outputs or displays one or more strings. The major differences between print() function and echo() function is that echo accepts an argument list and doesn't have a return value. Also, it is slightly...

1 minute read.

Python if-else

Python if-else The else statement is associated with if statement. An else statement executes if the conditional expression in if the statement becomes false or a Zero value. If the conditional expression...

2 minutes read.

PHP Indexed Array

In PHP, arrays are linear data structures that allow to store multiple elements of the same type under a single variable which helps in saving the time and effort of...

3 minutes read.

PHP is_null() Function

The is_null() function in PHP is used to  find whether the specified variable is a null or not. It returns a Boolean value true if the parameter var is null else it returns...

1 minute read.

PHP rtrim() Function

PHP rtrim() Function The rtrim() function in PHP is used to strip off the whitespace (or other characters) from the end of a string. If the second parameter is not specified, this function will strip are...

1 minute read.

PHP strtok() Function

PHP strtok() Function The strtok() function in PHP splits a string into smaller strings or tokenize the specified string.  Syntax strtok ( string $str , string $token ) Parameter str(required)- This parameter represents the input string to split into smaller strings or tokens. token(required)- It signifies...

1 minute read.

PHP is_int() Function

The PHP is_int() function in PHP is used to  find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it...

1 minute read.