I don't understand the sorting of array custom functions. Can any expert help me with the answer? Thank you.
漠漠2018-08-28 00:21:19
0
10
1330
Who can tell me what the values of these two parameters are and where they come from. Why can this function be sorted when written like this? I have been thinking about it all night and I don’t understand.
$var1 and $var2 are two adjacent key values in the $arr array. Each time, the two adjacent key values are compared, that is, key value 1 is first compared with key value 2, and after the comparison, the key value is 2 is compared with key value 3, and so on, all the way to the end of the array.
replyIn descending order, 1 means greater than, -1 means less than, 0 means equal to. If 1 means that the current two $var1 are larger than $var2, $var1 will be at the front, and $var2 will be at the back. Each time the two are next to each other. After a comparison, you can finally get the result. If you want to get ascending order, you only need to swap -1 and 1 in the return value to get ascending sorting.
$var1 and $var2 are two adjacent key values in the $arr array. Each time, the two adjacent key values are compared, that is, key value 1 is first compared with key value 2, and after the comparison, the key value is 2 is compared with key value 3, and so on, all the way to the end of the array.
This is just a built-in function.
usort is a built-in function in PHP. usort() allows users to sort arrays using custom functions.
usort compares two elements each time and obtains the return value for sorting. 0, -1, and 1 represent equal, less than, and greater respectively.