如何从字符串中获取具有所需长度的所有排列?

How to get all permutations with a desired length out of a string?

我有一个函数,我可以在其中传递一个字符串和所需的长度,以从字符串的字符中获取所有具有固定长度的排列。

但现在我想要整个单词的排列,例如

$source = "apple,potatoes,orange";

可悲的是,这个函数只给我字符的排列,而不是整个单词,我不知道如何修改代码,所以我会从上面的示例数据中得到这些排列:

apple,apple
apple,potatoes
apple,orange
potatoes,apple
potatoes,potatoes
potatoes,orange
//...

代码:

<?php 

$source = 'AaBbCcDdEe'; 

foreach(combos_with_repetition($source, 2) as $combo) { 
    echo "$combo<br>\n"; 
} 

function combos_with_repetition($input, $combo_len = 2) 
{ 
    for($i = 0; $i < $combo_len; ++$i) 
    { 
        @$part1 .= 'for($k'.$i.' = 0, $len = strlen($input); $k'.$i.' < $len; ++$k'.$i.') '; 
        @$part2 .= ($i?'.':'') . '$input[$k'.$i.']'; 
    } 
    eval($part1.'$rtn[] = '.$part2.';'); 
    return $rtn; 
} 

?>

所以任何帮助或提示如何修改代码都会有所帮助。

这应该可以为您完成工作:

function test($source){
    $source_array = explode(',',$source);
    $result = '';
   foreach($source_array as $item)
   {
       foreach($source_array as $item2){
            $result .= $item.','.$item2.'<br>';
       }
   }
return $result;
}

 $source="apple,patatoes,orange";
 echo test($source);

即使没有 evil().

,这也应该适合您

那么这段代码有什么作用?

1。有多少种排列?

很简单:

nl = amount of permutations

其中 n 是字数,l 是每个组合的所需长度。

因此对于这个特定示例,有 3 个单词(applepatatoesorange),我们希望每个排列的长度为 3。意思是:

33 = 27 permutations

2。将所有排列放在一起

我们循环遍历我们已经拥有的所有排列(从一个排列开始,一个 "empty permutation" ($permutations = [[]];)),对于每个排列,我们遍历我们的数据数组并组合每个排列与每个输入数据到一个新的排列。

现在我们这样做,直到我们得到每个排列所需的长度。

2.1 例子

Input data:

[1, 2]     //Input array with the data
length = 2 //Desired length for each permutation

                               //↓ new permutations for the next iteration
                               │
iteration 0:

    Permutations:
                  - []         │  -> []
                                  │
iteration 1:        ┌─────────────┤
                    │             │
    Permutations:   v             v
                  - []    + 1  │  -> [1]  
                  - []    + 2  │  -> [2]   
                                  │
iteration 2:        ┌─────────────┤
                    │             │
    Permutations:   v             v
                  - []    + 1  │  -> [1]
                  - []    + 2  │  -> [2]
                  - [1]   + 1  │  -> [1,1]  //desired length 2
                  - [1]   + 2  │  -> [1,2]  //desired length 2
                  - [2]   + 1  │  -> [2,1]  //desired length 2 
                  - [2]   + 2  │  -> [2,2]  //desired length 2
                               //↑ All permutations here

因此,正如您在上面的示例中所见,我们现在拥有所需长度的所有排列,此处为 2。

但为了仅获得具有所需长度的排列,我们每次迭代都会覆盖结果数组,因此最后结果数组中只有具有预期长度的排列。

3。代码:

<?php

    function getPermutations($input = [], $length = 2, $delimiter = ",") {
        $permutations = [[]];
        $data = is_array($input) ? $input : explode($delimiter, $input);

        for ($count = 0; $count < $length; $count++) {
            $tmp = [];
            foreach ($permutations as $permutation) {
                foreach ($data as $inputValue)
                    $tmp[] = array_merge($permutation, [$inputValue]);

            }
            $permutations = $tmp;
        }

        return $permutations;

    }


    $result = getPermutations("apple,patatoes,orange", 3);
    print_r($result);

?>

输出:

Array
(
    [0] => Array
        (
            [0] => apple
            [1] => apple
            [2] => apple
        )
    //...
    [26] => Array
        (
            [0] => orange
            [1] => orange
            [2] => orange
        )

)