Current Location: Home> Function Categories> uasort

uasort

Use user-defined comparison functions to sort values ​​in an array and keep index related
Name:uasort
Category:Array
Programming Language:php
One-line Description:Use a user-defined comparison function to sort keys and values ​​in an array.

Definition and usage

uasort() function uses a user-defined comparison function to sort the array and keep index related (no new keys assigned to elements).

Return TRUE if successful, otherwise return FALSE.

This function is mainly used to sort those combined arrays whose unit order is important.

Tip: Please use the uksort() function to sort the array key names through the user-defined comparison function.

Example

Use a user-defined comparison function to sort elements in the array $arr by key value:

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

$arr = array ( "a" => 4 , "b" => 2 , "c" => 8 , d => "6" ) ;
uasort ( $arr , "my_sort" ) ;
?>

Try it yourself

grammar

 uasort ( array , myfunction ) ;
parameter describe
array Required. Specifies the array to be sorted.
myfunction Optional. Defines a string that calls the comparison function. If the first parameter is less than or greater than the second parameter, the comparison function must return an integer less than or greater than 0.
Similar Functions
Popular Articles