zoukankan      html  css  js  c++  java
  • PHP 多个一维数组排列组合的所有可能

    <?php
        // auth bin.chen 525945448
    
    
        $a = array('A');
        $b = array('C');
        $c = array('E');
        $f = array('P');
        $d = array($a,$b,$c,$f );
        //计算每一个数组的长度
        $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 ++;
            //改变获取的位置
        }
    
    ?>
  • 相关阅读:
    shell——变量
    xxx is not in the sudoers file.This incident will be reported.的解决方法
    百度面试回忆
    iOS网络协议 HTTP/TCP/IP浅析
    使用xib封装一个自定义view的步骤
    修改了系统自带头文件后,Xcode会报错
    字典转模型规范化
    文本属性Attributes
    苹果API常用英语名词
    命令行 -- 命令"%cd%"
  • 原文地址:https://www.cnblogs.com/bin-pureLife/p/5239673.html
Copyright © 2011-2022 走看看