欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php-关于PHP中usort函数中,数值传递问题

程序员文章站 2022-05-03 18:03:30
...
phpphp函数原理求php高手

PHP手册中usort语法形式为 bool usort (array &array ,callback $cmp_function) 
请问 下例中 自定义函数中的$str1和$str2是按照什么顺序来接收数组中传来的数值,并怎么通过1,-1 ,0来排序?谢谢! 
例子:(把一个数组按照奇偶分开,再按照大小排列) 

if (($str1%2 == 0) && ($str2 == 0)){
if($str1>$str2) return -1; else return 1; }
if ($str1 %2 ==0) return 1;
if ($str2 %2 ==0) return -1;
return ($str2>$str1)? 1: -1; } $scores=array(64,78,51,86,97,60,87,91,100,17,62); usort($scores, 'Compare');
print_r($scores);
?>

这是输出结果: 
Array([0] => 97 [1] => 91 [2] => 87 [3] => 51 [4] => 17 [5] => 86 [6] => 64 [7] => 78 [8] => 100 [9] => 60 [10] => 62)