如何在PHP

时间:2016-08-14 02:03:30

标签: php arrays combinations

我在Stackoverflow上阅读了与此主题相关的大约15个其他资源和答案,并且只找到了一个与我想要的相近的代码,但我不知道如何进一步完善它的生成。

这是我到目前为止所做的:     

$combos = array_unique($letters);
$lastres = $letters;
for ($i = 1; $i < count($letters); $i++) {
$newres = array();
foreach ($lastres as $r) {
    foreach ($letters as $let) {
    $new = $r . $let;
            if (!in_array($new, $newres)) {
                $newres[] = $new;
                // Action
                $combos[] = $new;
            }
        }
    }
    $lastres = $newres;
  }
  print_r($combos);
  ?>

以上代码从输入&#34; apple&#34;生成1365个结果,但它包含不需要的结果。

我将在此处粘贴前86个结果:

[0] =&gt; a [1] =&gt; p [3] =&gt; l [4] =&gt; e [5] =&gt; aa [6] =&gt; ap [7] =&gt; al [8] =&gt; ae [9] =&gt; pa [10] =&gt; pp [11] =&gt; pl [12] =&gt; pe [13] =&gt; la [14] =&gt; lp [15] =&gt; ll [16] =&gt; le [17] =&gt; ea [18] =&gt; ep [19] =&gt; el [20] =&gt; ee [21] =&gt; aaa [22] =&gt; aap [23] =&gt; aal [24] =&gt; aae [25] =&gt; apa [26] =&gt; app [27] =&gt; apl [28] =&gt;猿[29] =&gt; ala [30] =&gt; alp [31] =&gt;全部[32] =&gt; ale [33] =&gt; aea [34] =&gt; aep [35] =&gt; ael [36] =&gt; aee [37] =&gt; paa [38] =&gt; pap [39] =&gt; pal [40] =&gt; pae [41] =&gt; ppa [42] =&gt; ppp [43] =&gt; ppl [44] =&gt; ppe [45] =&gt; pla [46] =&gt; plp [47] =&gt; pll [48] =&gt; ple [49] =&gt;豌豆[50] =&gt; pep [51] =&gt; pel [52] =&gt;小便[53] =&gt; laa [54] =&gt; lap [55] =&gt; lal [56] =&gt; lae [57] =&gt; lpa [58] =&gt; lpp [59] =&gt; lpl [60] =&gt; lpe [61] =&gt; lla [62] =&gt; llp [63] =&gt; lll [64] =&gt; lle [65] =&gt; lea [66] =&gt; lep [67] =&gt; lel [68] =&gt; lee [69] =&gt; eaa [70] =&gt; eap [71] =&gt; eal [72] =&gt; eae [73] =&gt; epa [74] =&gt; epp [75] =&gt; epl [76] =&gt; epe [77] =&gt; ela [78] =&gt; elp [79] =&gt; ell [80] =&gt; ele [81] =&gt; eea [82] =&gt; eep [83] =&gt;鳗鱼[84] =&gt; eee [85]

世界苹果包含1 A,1 L和1 E,但它包含的结果如&#34; aa&#34;,&#34; ll&#34;,&#34; ee&#34;,& #34; aaa&#34;,&#34; lll&#34;,&#34; eee&#34;。我不希望这些包括在我的结果中。

我遇到的另一个问题是我只需要一个特定大小的字母组合的实例(不论顺序如何)。例如:当它生成3个字母组合时,我只需要&#34; ple&#34;而不是&#34; pel&#34;,&#34; lpe&#34;,&#34; lep&#34;,&#34; epl&#34;或&#34; elp&#34;,4个字母,5等相同

我不确定我是否正确接近这个或者我是否正在解释我的意思。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您正在寻找的东西IMO是一个强大的功能集:

$ cat a.php
<?php
$result = array_values(array_unique(str_split('apples')));

function getPowerSet($str_array) {
    $set_size = count($str_array);
    $pow_set_size = pow(2, $set_size);
    $return = array();
    for($counter = 0; $counter < $pow_set_size; $counter++) {
      $tmp_str = "";
      for($j = 0; $j < $set_size; $j++) {
          if($counter & (1 << $j)) {
              $tmp_str .= "$str_array[$j]";
          }
      }
      $return[] = $tmp_str;
    }
   return $return;
}
print_r(getPowerSet($result));
?>
$ php a.php
Array
(
    [0] =>
    [1] => a
    [2] => p
    [3] => ap
    [4] => l
    [5] => al
    [6] => pl
    [7] => apl
    [8] => e
    [9] => ae
    [10] => pe
    [11] => ape
    [12] => le
    [13] => ale
    [14] => ple
    [15] => aple
    [16] => s
    [17] => as
    [18] => ps
    [19] => aps
    [20] => ls
    [21] => als
    [22] => pls
    [23] => apls
    [24] => es
    [25] => aes
    [26] => pes
    [27] => apes
    [28] => les
    [29] => ales
    [30] => ples
    [31] => aples
)

如果你想要重复字母,例如苹果中的2'p,那么做一个小改动:

$result = str_split('apples');

然后结果将是:

$ php index.php
Array
(
    [0] => 
    [1] => a
    [2] => p
    [3] => ap
    [4] => p
    [5] => ap
    [6] => pp
    [7] => app
    [8] => l
    [9] => al
    [10] => pl
    [11] => apl
    [12] => pl
    [13] => apl
    [14] => ppl
    [15] => appl
    [16] => e
    [17] => ae
    [18] => pe
    [19] => ape
    [20] => pe
    [21] => ape
    [22] => ppe
    [23] => appe
    [24] => le
    [25] => ale
    [26] => ple
    [27] => aple
    [28] => ple
    [29] => aple
    [30] => pple
    [31] => apple
    [32] => s
    [33] => as
    [34] => ps
    [35] => aps
    [36] => ps
    [37] => aps
    [38] => pps
    [39] => apps
    [40] => ls
    [41] => als
    [42] => pls
    [43] => apls
    [44] => pls
    [45] => apls
    [46] => ppls
    [47] => appls
    [48] => es
    [49] => aes
    [50] => pes
    [51] => apes
    [52] => pes
    [53] => apes
    [54] => ppes
    [55] => appes
    [56] => les
    [57] => ales
    [58] => ples
    [59] => aples
    [60] => ples
    [61] => aples
    [62] => pples
    [63] => apples
)

在此处详细了解该算法:http://www.geeksforgeeks.org/power-set/