PHP5入门之分组算法
发布时间:2018-05-23 编辑:jiaochengji.com
本文介绍了php分组算法的小例子,把一组数据拆分为6组输出算法,有需要的朋友参考下。
例子,把一组数据拆分为6组输出算法。
代码:
复制代码 代码示例:
<?php
$groupCount = 6; //组数
$userIdList = array(1, 2, 3, 4, 5, 6, 7, 8, 9,10);
$size = count($userIdList );
$sizeGroupPer = floor($size / $groupCount );//每组被分配的个数
$criticalValue = $size % $groupCount ; //临界值
$startIndex = 0;
$endIndex = 0;
for ($i = 0; $i < $groupCount ; $i++ ) {
if ($i < $critical) { //表示哪些组可以被多分配
$endIndex = $startIndex + $sizeGroupPer + 1;
} else {
$endIndex = $startIndex + $sizeGroupPer ;
} // www.jbxue.com
$strGroup = "";
for ($j = $startIndex ; $j < $endIndex; $j++) {
if (($j + 1) == $to) {
$strGroup .= $arryData[$j];
} else {
$strGroup .= $arryData[$j].",";
}
}
$startIndex = $endIndex;
echo "第 ".$i." 组数据::".$strGroup;
}
$groupCount = 6; //组数
$userIdList = array(1, 2, 3, 4, 5, 6, 7, 8, 9,10);
$size = count($userIdList );
$sizeGroupPer = floor($size / $groupCount );//每组被分配的个数
$criticalValue = $size % $groupCount ; //临界值
$startIndex = 0;
$endIndex = 0;
for ($i = 0; $i < $groupCount ; $i++ ) {
if ($i < $critical) { //表示哪些组可以被多分配
$endIndex = $startIndex + $sizeGroupPer + 1;
} else {
$endIndex = $startIndex + $sizeGroupPer ;
} // www.jbxue.com
$strGroup = "";
for ($j = $startIndex ; $j < $endIndex; $j++) {
if (($j + 1) == $to) {
$strGroup .= $arryData[$j];
} else {
$strGroup .= $arryData[$j].",";
}
}
$startIndex = $endIndex;
echo "第 ".$i." 组数据::".$strGroup;
}
您可能感兴趣的文章:
PHP5入门之分组算法
php入门教程(索引)
php5与php7的区别是什么?
php5和php7垃圾回收上的区别是什么?
适合入门的php基础系列教程
php7和php5区别是什么
php7的版本和5的版本有哪些不同?
php7与php5的区别大吗
php入门实例查询数据库记录
PHP5入门之读写文本文件
[关闭]