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