Latest web development tutorials

PHP usort () function

PHP Array Reference Complete PHP Array Reference

Examples

Using a user-defined comparison function of elements in the array $ a sort: Sort the elements of the $ a array using a user-defined comparison function:

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

$a=array(4,2,8,6);
usort($a,"my_sort");
?>

Running instance »

Definition and Usage

usort () the use of user-defined comparison function to sort the array.

grammar

usort( array,myfunction );

参数 描述
array 必需。规定要排序的数组。
myfunction 可选。一个定义了可调用比较函数的字符串。如果第一个参数 <, =, > 第二个参数,相应地比较函数必须返回一个 <, =, > 0 的整数。

technical details

return value: If successful it returns TRUE, on failure returns FALSE.
PHP version: 4+


PHP Array Reference Complete PHP Array Reference