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

WBOY
Release: 2016-06-02 11:31:22
Original
1405 people have browsed it

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)

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template