Home >> PHP >> usort() function in PHP

usort() function in PHP

The usort() function used for sorts an array by values using a user defined function.

Syntax

usort(array,yourfunction);

Example

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

$array=array(1,3,2,4);
usort($array,"cmpfun");
print_r($array);

Output

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

Post Your Comment

Next Questions
What is the difference between array_combine() and array_merge()
What are the differences between array_diff(), array_diff_assoc() and array_diff_key()
What is the difference between array_intersect_assoc() and array_intersect_key()
What is difference between array_push() and array_pop()
What is difference between array_product() and array_sum()
What is the difference between session and cookie
What is session & work
What are data types
What are super global variable
$GLOBALS super global variable
$_SERVER super global variable
$_REQUEST super global variable
$_POST super global variable
$_GET super global variable
$_FILES super global variable
$_ENV super global variable
$_COOKIE super global variable
$_SESSION super global variable
Ternary operator
Variable types
What is password encrypt and decrypt function
Difference between echo and print
How can destroy and unset the variable of a session
Function
isset() function

Copyright ©2022 coderraj.com. All Rights Reserved.