Php Implements Random Sorting of data by weight, php achieves sorting _ PHP Tutorial

Source: Internet
Author: User
Php Implements Random Sorting of data by weight, and php performs sorting. Php is used to sort data by weight randomly. php is used to sort data by weight. this example describes how php can sort data by weight randomly. Share it with you for your reference. Php Implements Random Sorting of data by weight.

This example describes how php can sort data randomly by weight. Share it with you for your reference.

The specific implementation method is as follows:

The code is as follows:


<? Php
/**
* @ Param array $ weight, for example, array ('a' => 10, 'B' => 20, 'C' => 50)
* @ Return string key name
*/
Function roll ($ weight = array ()){
$ Roll = rand (1, array_sum ($ weight ));
$ _ TmpW = 0;
$ Rollnum = 0;
Foreach ($ weight as $ k => $ v ){
$ Min = $ _ tmpW;
$ _ TmpW + = $ v;
$ Max = $ _ tmpW;
If ($ roll> $ min & $ roll <= $ max ){
$ Rollnum = $ k;
Break;
}
}
Return $ rollnum;
}

$ Row = roll (array ('a' => 10, 'B' => 20, 'C' => 50 ));
Echo $ row;
?>

I hope this article will help you with php programming.

Examples in this article describes how php performs random sorting of data by weight. Share it with you for your reference. Details...

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.