PHP头条
热点:

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


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)

www.phpzy.comtrue/php/1630.htmlTechArticlephp-关于PHP中usort函数中,数值传递问题 phpphp函数原理求php高手 PHP手册中usort语法形式为 bool usort (array else return 1; } if ($str1 %2 ==0) return 1; if ($str2 %2 ==0) return -1; return ($str2>$str1)? 1: -1; } $...

相关文章

相关频道:

PHP之友评论

今天推荐