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
1269

QQ图片20180828002804.png

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.

漠漠
漠漠

reply all(4)
YHF

$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.

  • reply Every time a comparison is made, it always returns 1 or -1 or 0, right? So how did he get this sequence in the end?
    漠漠 author 2018-08-30 16:06:53
  • reply In 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.
    YHF author 2018-08-30 16:22:46
秋香姐家的小书童

This is just a built-in function.

  • reply What do you mean? We just need to do it according to this method. We don’t need to know how to calculate it, right?
    漠漠 author 2018-08-28 17:22:11
墨尘

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.

  • reply Is it similar to bubble sort?
    漠漠 author 2018-08-28 15:16:37
White secretary

    

<?php 
echo "我可以和你们成为好朋友吗 哭唧唧";
>


  • reply are you laughing at me
    漠漠 author 2018-08-28 15:12:13
  • reply I'm... just joking... I'm new to programming... Brother... don't be angry... QAQ
    White secretary author 2018-08-28 16:22:59
  • reply Haha, I’m new to this too
    漠漠 author 2018-08-28 16:25:35
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!