PHP 多个数组的排列组合
$a=array(1,2,3,4);$b=array('a','b','c');假设有两个数组如上,组合的效果如下1a,1b,1c2a,2b,2c3a,3b,3c4a,4...
$a = array(1,2,3,4);
$b = array('a','b','c');
假设有两个数组如上,组合的效果如下
1a,1b,1c
2a,2b,2c
3a,3b,3c
4a,4b,4c
求实现方法
当然可能出现一个或者多个数组的情况,比如
$a = [1,2,3,4];
$b = ['a', 'b', 'c'];
$c = [8,9,0];
$d = ['x','y','z']; 展开
$b = array('a','b','c');
假设有两个数组如上,组合的效果如下
1a,1b,1c
2a,2b,2c
3a,3b,3c
4a,4b,4c
求实现方法
当然可能出现一个或者多个数组的情况,比如
$a = [1,2,3,4];
$b = ['a', 'b', 'c'];
$c = [8,9,0];
$d = ['x','y','z']; 展开
展开全部
$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 ++;
//改变获取的位置
}
$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 ++;
//改变获取的位置
}
展开全部
两个排列,如果多个可以考虑做对象!
$a=array(1,2,3,4,5,6,7,8,9,10);
$b=array('a','b','c','d');
$c1 = count($a);
$c2 = count($b);
if($c1 > $c2){
$c = $c1;
}else{
$c = $c2;
}
for($i=0;$i<$c;$i++){
echo @$d[$i] = $a[$i].$b[$i]."<hr/>";
}
$a=array(1,2,3,4,5,6,7,8,9,10);
$b=array('a','b','c','d');
$c1 = count($a);
$c2 = count($b);
if($c1 > $c2){
$c = $c1;
}else{
$c = $c2;
}
for($i=0;$i<$c;$i++){
echo @$d[$i] = $a[$i].$b[$i]."<hr/>";
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这不是多重循环吗?
foreach ($a as $ $aa){
foreach ($b as $bb){
echo "$aa$bb,";
}
echo "\n";
}
foreach ($a as $ $aa){
foreach ($b as $bb){
echo "$aa$bb,";
}
echo "\n";
}
追问
。。。这么简单还排列个毛啊,两个数组两个循环 如果三个呢?如果三十个呢?三百个呢?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-05-10
展开全部
递归,我也在找这个答案!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询