
正文
php数据组合凑整 php组合算法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
PHP 多个数组的排列组合
$a = array(1,2,3,4,5,6,7);
$b = array(1,2,3);
$c = array(1,2,3,4,5,6,7,8,9,10);
$d = array($a,$b,$c);
//计算每一个数组的长度
$len = 1;
$arrLen = count($d); //需要排列数组有多少个
$recIndex = null; //记录当前该取的位置
//foreach 计数
$count_3 = 0;
foreach ($d as $key = $value) {
$lenRec[$count_3] = count($value);
$len = $lenRec[$count_3]*$len;
$recIndex[] = 0;//第一次全部取第0个
$count_3++;
}
//算出% 的值
$count = 1;
foreach ($lenRec as $key = $value) {
$moduloVal = 1;
if($arrLen == $count){
$modulo[] = count($d[$arrLen-1]); //等于最后一个的长度
}else{
$count_1 = 1;
foreach ($lenRec as $index = $item) {
$count_1 $count $moduloVal = $moduloVal*$item;
$count_1 ++;
}
$modulo[] = $moduloVal;
}
$count ++;//为了防止$d key是有值的 不是自然序列 需要计数
}
$i = 1;
while ( $i = $len) {
$html = '';
$count_2 = 0;// 取模
$temp = '';
foreach ($d as $value) {
$html .= $value[$recIndex[$count_2]%$lenRec[$count_2]].",";
$count_2 ++;
}
echo $html."br";
foreach ($modulo as $key = $value) {
if($i%$value == 0 $key $arrLen - 1 ){
$recIndex[$key] = $recIndex[$key] +1;
}
if($key == $arrLen - 1){
if($i%$value == 0){
$recIndex[$key] = 0;
}else{
$recIndex[$key] = $recIndex[$key] +1;
}
}
}
$i ++;
//改变获取的位置
}
相关问答
Q1: 怎么用PHP代码做出数字1到10的所有组合方式?
你的思路是
先随机得到一个1-100的i
然后截取判断php数据组合凑整,再在判断里循环?
先改思路,在不考虑效率的情况下php数据组合凑整,先循环
$arr=Array();
while(count($arr)10){
$temp=rand(1,100);
//判断末尾数字
//判断是否数组中存在,不存在就$arr[]=$temp;存在就continue;
}
如果考虑效率,则修改算法,1-100就是两位数,随机生成十位数,末位再随机生成3和8比较好
Q2: php中的两组数如何相间组合到一起
1.php数据组合凑整你先选择php数据组合凑整你想要组合php数据组合凑整的图形 2.右击选择弹出php数据组合凑整的“组合图形” 3.保存文件。
php数据组合凑整的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php组合算法、php数据组合凑整的信息别忘了在本站进行查找喔。







