Example, split a set of data into 6 sets of output algorithms.
Code:
-
-
- $groupCount = 6; //Number of groups
- $userIdList = array(1, 2, 3, 4, 5, 6, 7, 8, 9,10);
- $size = count($userIdList );
- $sizeGroupPer = floor($size / $groupCount );//The number of allocated items in each group
- $criticalValue = $size % $groupCount; //Critical value
- $startIndex = 0;
- $endIndex = 0;
- for ($i = 0; $i < $groupCount ; $i++ ) {
- if ($i < $critical) { //Indicates which groups can be allocated more
- $endIndex = $startIndex + $sizeGroupPer + 1;
- } else {
- $endIndex = $startIndex + $sizeGroupPer ;
- } // bbs.it-home.org
-
- $strGroup = "";
- for ($j = $startIndex ; $j < $endIndex; $j++) {
- if (($j + 1) == $to) {
- $strGroup .= $arryData[$j];
- } else {
- $strGroup .= $arryData[$j ].",";
- }
- }
- $startIndex = $endIndex;
- echo "The ".$i." group data: ".$strGroup;
- }
Copy code
|