PHP中数组中字符串的所有变体

PHP中数组中字符串的所有变体,php,string,text-parsing,Php,String,Text Parsing,我有一个带空格的字符串。我需要拆分(分解)它并从中获得所有序列的变体。例如: string1 string2 string3 string1 string2 string3 我需要解析它并获得如下输出: string1 string2 string3 string1 string3 string2 string2 string1 string3 string2 string3 string1 string3 string2 string1 string3 string1 string2 stri

我有一个带空格的字符串。我需要拆分(分解)它并从中获得所有序列的变体。例如:

string1 string2 string3 string1 string2 string3 我需要解析它并获得如下输出:

string1 string2 string3 string1 string3 string2 string2 string1 string3 string2 string3 string1 string3 string2 string1 string3 string1 string2 string1 string2 string3 string1 string3 string2 string2 string1 string3 string2 string3 string1 string3 string2 string1 string3 string1 string2 最有效的方法是什么?
编辑:实际上我最多需要解析3个字符串。因此,我这样做的方式并不漂亮(硬编码):

$exploded\u query=exploded(“”,$query); if(计数($U查询)==2){ //2种变体 } if(计数($U查询)==3){ //6种变体 }
因此,我正在寻找一种很好的方法来做这件事。

我绝不声称这是有效的或最优的。还有更好的解决方案。但这只是对你问题的直接回答。如果要消除一些膨胀(可能会牺牲一点性能),可以将
getRemainingWords
函数调用替换为:

$index = 0;
array_values(array_filter($words, function($key, &$index) { return !($key == $index++); }));
否则,就在这里

function getPossibleCombinations($words) {
    $combinations = array();
    $count = count($words);

    // Base case: if there's only 1 word, there's only one combination
    if ($count == 1) {
        return array($words);
    }

    // Otherwise, loop over each words
    foreach ($words as $key=>$word) {

        // For each item, get all of the remaining items in the array (all except the current one)
        $otherWords = getRemainingWords($words, $key);

        // And recursively permute them
        $otherCombinations = getPossibleCombinations($otherWords);
        foreach ($otherCombinations as $otherCombination) {
            $combinations[] = array_merge(array($word), $otherCombination);
        }
    }

    return $combinations;
}


function getRemainingWords($array, $index) {
    $results = array();

    foreach ($array as $key=>$value) {
        if ($key == $index) {
            continue;
        }

        $results[] = $value;
    }

    return $results;
}

它是数组的排列


看这里->,这对你很有帮助。

我认为你真的应该用这种方法问你想回答的任何问题。没有有效的方法可以做到这一点,因为您已经声明需要每个组合(某些算法的最有效实现将智能地选择需要实际运行的组合,并且在可能的情况下它们将短路)。这意味着“最有效”的方法仍然是n!(n阶乘)。仅仅10个单词就已经有300万个组合。你有任何代码要发布吗?是的,发布了代码。请看:
function getPossibleCombinations($words) {
    $combinations = array();
    $count = count($words);

    // Base case: if there's only 1 word, there's only one combination
    if ($count == 1) {
        return array($words);
    }

    // Otherwise, loop over each words
    foreach ($words as $key=>$word) {

        // For each item, get all of the remaining items in the array (all except the current one)
        $otherWords = getRemainingWords($words, $key);

        // And recursively permute them
        $otherCombinations = getPossibleCombinations($otherWords);
        foreach ($otherCombinations as $otherCombination) {
            $combinations[] = array_merge(array($word), $otherCombination);
        }
    }

    return $combinations;
}


function getRemainingWords($array, $index) {
    $results = array();

    foreach ($array as $key=>$value) {
        if ($key == $index) {
            continue;
        }

        $results[] = $value;
    }

    return $results;
}