An interpretation of the Usort () function in PHP

Source: Internet
Author: User
Tags array sort

Recently learned to encounter a custom array sort function usort () Some do not understand, search a lot of places are not very good explanation, their research for a long time, hair and I like the same beginners share ~

BOOL usort ( array &$array , callable $cmp_function )

The function makes its own custom sort for the array, and the collation consists $cmp_function 定义。 of a return value of ture or false.

Now let's analyze a simple function:

1<?PHP2 functionRe$a,$b){3     return($a<$b)? 1:-1;4 }5 $x=Array(1,3,2,5,9);6 Usort($x, ' re ');7 Print_r($x);8?>

Printing results are:

Array (    [0] = 9    [1] = 5    [2] = 3    [3] = 2    [4] = 1
)

The array is implemented in reverse order. The analysis is as follows:

Usort 22 extracts the values in the array and enters the custom function sequentially, the custom function returns 1 according to the content, or -1;usort according to the return value of 1 or-1, gets the value passed in 1 "greater than" or "less than" the value 2, and then the value is ordered from small to large. that is: The return value is 1, the value 1 "is greater than" the value 2, and then sort: The value 2-> the value 1; The return value is-1, the value 1 "is less than" the value 2, and then sorts: the value 1-> the value 2.

In the above custom function, $a < $b If 1 is returned correctly, the $ A "greater than" $b is sorted by the order $b-> $a, and if the error returns-1, the description of $ A "less than" $b is sorted by the order $a-> $b.

The following is a more complex sort: An array is preceded by odd, then the order is made from large to small.

1 functionCompare ($str 1,$str 2) {2     if(($str 1% 2 = = 0) && ($str 2%2 = = 0)) {3         if($str 1>$str 2)4             return-1;5         Else6             return1;7     }8     if($str 1% 2 = = 0)9         return1;Ten     if($str 2% 2 = = 0) One         return-1; A     return($str 2>$str 1) ? 1:-1; - } - $scores=Array(22,57,55,12,87,56,54,11); the Usort($scores, ' Compare ' ); - Print_r($scores);

The implementation steps are:

1 "Determine whether the input two values are even, are even, from large to small sorting;

2 If not all are even, then at least one is odd, first determine whether $STR1 is an even number, if an even number, namely: if ($str 1%2==0) is established, then return 1, meaning $str1 "greater than" $str 2, the Usort function is ordered as "small" $str 2-> " Large "$str 1 (even);

3 "If the $str1 is odd, the above does not return any value, and then determine whether $STR2 is even, if it is even, then return-1, meaning $str1" less than "$str 2, the Usort function is ordered to" small "$str 1 (odd)," big "$ STR2 (even);

4 "If two values are odd, then no value is returned above, then the $STR1 and $str2 are sorted from large to small;

The output is:

Array (    [0] [+] [    1] = [2] = [    3    ] = [4] [5] = [6] [+]/[+]/+]    [7] = 12)

Above, the whole function finished, smooth to the ideal result.

Although a little pediatrics for the great God, but run through this function, still full of excitement!

An interpretation of the Usort () function in PHP

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.