使用PHP生成所有可能的组合

时间:2018-11-02 15:17:17

标签: php arrays combinators

我有一个带下划线的数字序列:

_10_1_18_4_9_14_ _

我想用字母代替下划线而不碰数字,并生成组合的完整列表,但我不知道该怎么做。

现在我有这个:

$alph = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
echo $alph[0]."10".$alph[0]."1".$alph[0]."18".$alph[0]."4".$alph[0]."9".$alph[0]."14".$alph[0] .$alph[0];

现在,我不知道如何探索所有可能的组合。

有208 827 064 576可能的组合,我必须将它们全部放在.txt文件中。这就是为什么我避免使用“ for”

有什么想法吗?

编辑:例如,在循环中使用$ i ++,我们可以将组合数量限制为100。

我知道这将需要很多时间和空间

2 个答案:

答案 0 :(得分:2)

您可以利用以下事实,而不是使用大量嵌套循环:应用于字符串的++运算符将环绕并生成所需的序列集。

$str = '_10_1_18_4_9_14_ _';
$letters = str_repeat('A', substr_count($str, '_'));

$limit = 100;

for ($i = 0; $i < $limit; $i++) {
    echo interpolate($str, str_split($letters)), PHP_EOL;
    $letters++;
}

function interpolate($str, $letters) {
    while (($pos = strpos($str, '_')) !== false) {
        $str[$pos] = array_shift($letters);
    }

    return $str;
}

这将问题分解为通用函数,该通用函数用字母数组替换字符串中的连续下划线,并为每次迭代简单地将字符串从AAAAA...开始递增。

有关演示,请参见https://3v4l.org/GAEJN

答案 1 :(得分:1)

这可能不是很有效,但是可以完成工作:

<?php
$alph = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$a = array();

foreach( $alph as $l1 )
{
    $a[0] = $l1.'10';
    foreach( $alph as $l2 )
    {
        $a[1] = $l2.'1';
        foreach( $alph as $l3 )
        {
            $a[2] = $l3.'18';
            foreach( $alph as $l4 )
            {
                $a[3] = $l4.'4';
                foreach( $alph as $l5 )
                {
                    $a[4] = $l5.'9';
                    foreach( $alph as $l6 )
                    {
                        $a[5] = $l6.'14';
                        foreach( $alph as $l7 )
                        {
                            $a[6] = $l7;
                            foreach( $alph as $l8 )
                            {
                                $a[7] = ' '.$l8; // your example shows a space, not sure if that was intentional
                                // $line = implode( '', $a )."\r\n";
                                // Write $line to a file
                            }
                        }
                    }
                }
            }
        }
    }
}

示例输出:

A10A1A18A4A9A14A A
A10A1A18A4A9A14A B
A10A1A18A4A9A14A C
A10A1A18A4A9A14A D
A10A1A18A4A9A14A E
A10A1A18A4A9A14A F
A10A1A18A4A9A14A G
A10A1A18A4A9A14A H
A10A1A18A4A9A14A I
A10A1A18A4A9A14A J
A10A1A18A4A9A14A K
A10A1A18A4A9A14A L
A10A1A18A4A9A14A M
A10A1A18A4A9A14A N
A10A1A18A4A9A14A O
A10A1A18A4A9A14A P
A10A1A18A4A9A14A Q
A10A1A18A4A9A14A R
A10A1A18A4A9A14A S
A10A1A18A4A9A14A T
A10A1A18A4A9A14A U
A10A1A18A4A9A14A V
A10A1A18A4A9A14A W
A10A1A18A4A9A14A X
A10A1A18A4A9A14A Y
A10A1A18A4A9A14A Z
A10A1A18A4A9A14B A
A10A1A18A4A9A14B B
A10A1A18A4A9A14B C
A10A1A18A4A9A14B D
A10A1A18A4A9A14B E
A10A1A18A4A9A14B F
A10A1A18A4A9A14B G
A10A1A18A4A9A14B H
A10A1A18A4A9A14B I
A10A1A18A4A9A14B J
A10A1A18A4A9A14B K
A10A1A18A4A9A14B L
A10A1A18A4A9A14B M
A10A1A18A4A9A14B N
A10A1A18A4A9A14B O
A10A1A18A4A9A14B P
A10A1A18A4A9A14B Q
A10A1A18A4A9A14B R
A10A1A18A4A9A14B S
A10A1A18A4A9A14B T
A10A1A18A4A9A14B U
A10A1A18A4A9A14B V
A10A1A18A4A9A14B W
A10A1A18A4A9A14B X
A10A1A18A4A9A14B Y
A10A1A18A4A9A14B Z
A10A1A18A4A9A14C A
A10A1A18A4A9A14C B
A10A1A18A4A9A14C C
A10A1A18A4A9A14C D
A10A1A18A4A9A14C E
A10A1A18A4A9A14C F
A10A1A18A4A9A14C G
A10A1A18A4A9A14C H