PHP 5 Getting Started grouping algorithm (i)
Splitting a set of data into 6 sets of output algorithms
?
$groupCount = 6; Number of groups
$userIdList = Array (1, 2, 3, 4, 5, 6, 7, 8, 9,10);
$size = count ($userIdList);
$sizeGroupPer = Floor ($size/$groupCount);//number of allocations per group
$criticalValue = $size% $groupCount; Critical value
$startIndex = 0;
$endIndex = 0;
for ($i = 0; $i < $groupCount; $i + +) {?
?
??????????? if ($i < $critical) {//indicates which groups can be multi-allocated
?????????????? $endIndex = $startIndex + $sizeGroupPer + 1;
????????????} else {
??????????????? $endIndex = $startIndex + $sizeGroupPer;
????????????}
??
???????????? $strGroup = "";?
??????????? for ($j = $startIndex; $j < $endIndex; $j + +) {
?????????????? if (($j + 1) = = = $to) {
?????????????????? $strGroup. = $arryData [$j];
?????????????? } else {
?????????????????? $strGroup. = $arryData [$j]. ",";???????????
?????????????? }
??????????? }
?
?????????? $startIndex = $endIndex;
???????? Echo? " Section ". $i." Group data:: ". $strGroup;?
}
?