Php-about the value transfer in the usort function in PHP

Source: Internet
Author: User
In the PHP manual, the usort syntax is boolusort (array & amp; array, callback $ cmp_function) in the next example, in the custom functions, $ str1 and $ str2 receive the values in the array in the order in which they are sorted by 1,-, and 0? Thank you! Example: (separate an array by parity and then sort it by size) $ str2) return-1; elsereturn1;} if ($ str1 % 20) return1; if ($ str2 % 20) how php functions work

In the PHP manual, the usort syntax is bool usort (array & array, callback $ cmp_function)
In the following example, in the user-defined functions, $ str1 and $ str2 receive the values in the array in the order in which they are sorted by 1,-1, 0? Thank you!
Example: (separate an array by parity and sort it by size)

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 );
?>

This is the output result:
Array ([0] => 97 [1] => 91 [2] => 87 [3] => 51 [4] => 17 [5] => 86 [6] => 64 [7] => 78 [8] => 100 [9] => 60 [10] => 62)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.