Home > Backend Development > PHP Tutorial > Grouping Algorithm for Getting Started with PHP5

Grouping Algorithm for Getting Started with PHP5

WBOY
Release: 2016-07-25 09:12:51
Original
899 people have browsed it

Example, split a set of data into 6 sets of output algorithms. Code:

  1. $groupCount = 6; //Number of groups
  2. $userIdList = array(1, 2, 3, 4, 5, 6, 7, 8, 9,10);
  3. $size = count($userIdList );
  4. $sizeGroupPer = floor($size / $groupCount );//The number of allocated items in each group
  5. $criticalValue = $size % $groupCount; //Critical value
  6. $startIndex = 0;
  7. $endIndex = 0;
  8. for ($i = 0; $i < $groupCount ; $i++ ) {
  9. if ($i < $critical) { //Indicates which groups can be allocated more
  10. $endIndex = $startIndex + $sizeGroupPer + 1;
  11. } else {
  12. $endIndex = $startIndex + $sizeGroupPer ;
  13. } // bbs.it-home.org
  14. $strGroup = "";
  15. for ($j = $startIndex ; $j < $endIndex; $j++) {
  16. if (($j + 1) == $to) {
  17. $strGroup .= $arryData[$j];
  18. } else {
  19. $strGroup .= $arryData[$j ].",";
  20. }
  21. }
  22. $startIndex = $endIndex;
  23. echo "The ".$i." group data: ".$strGroup;
  24. }
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template