usort

usort -- Sort an array using a user-define comparison function

Description

void usort(array array, function cmp_function);

This function will sort an array using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.

Example 1. usort() example

function cmp($a,$b) {   
    if($a==$b) return 0;
    return ($a>$b) ? -1:1;
}
$a = array(3,2,5,6,1);
usort($a,cmp);
while(list($key,$value) = each($a)) {
    echo "$key: $value\n";
}
This example would display: 0: 6 1: 5 2: 3 3: 2 4: 1 Obviously in this trivial case the rsort() function would be more appropriate.

See also arsort(), asort(), ksort(), rsort() and sort().




月光软件程序下载编程文档电脑教程网站设计网址导航网络文学游戏天地幽默笑话生活休闲写作范文安妮宝贝
电脑技术编程开发网络专区谈天说地情感世界游戏元素分类游戏热门游戏体育运动手机专区业余爱好影视沙龙
音乐天地数码广场教育园地科学大观古今纵横谈股论金人文艺术医学保健动漫图酷二手专区地方风情各行各业

月光软件站·版权所有