php – 如何返回给定字符串的所有组合? (例如’foo bar’= bar,bar_foo,foo)

此问题与上述建议的问题不重复.标题可能听起来相似,但其答案绝不会导致下面问题中描述的结果.

我很难递归地迭代未知长度的数组来创建独特的字符串组合.你能帮我吗?

目标是采用像foo bar这样的字符串,并从该字符串创建唯一的组合:

foo
bar
bar_foo (alphabetized to make unique combinations, not permutations)

另一个例子:

车栏添加应返回:

add
add_bar
add_car
add_bar_car
bar
bar_car
car

这是我的进步:

function string_builder($length) {
    $arrWords = array('add','bar','car','dew','eat','fat','gym','hey','ink','jet','key','log','mad','nap','odd','pal','qat','ram','saw','tan','urn','vet','wed','xis','yap','zoo');
    $arr = array();
    for ($i=0; $i < $length; $i++) { 
        $arr[] = $arrWords[$i];
    }
    return implode(' ', $arr);
}
function get_combinations($string) {
    $combinations = array(); // put all combinations here
    $arr = explode(' ',$string);
    $arr = array_unique($arr); // only unique words are important
    sort($arr); // alphabetize to make unique combinations easier (not permutations)
    $arr = array_values($arr); // reset keys
    for ($i=0; $i < count($arr); $i++) {
        // this is where I'm stuck
        // how do I loop recursively through all possible combinations of an array?
    }
    return $combinations;
}
// Test it!
for ($i=1; $i < 26; $i++) { 
    $string = string_builder($i);
    $combinations = get_combinations($string);
    echo $i . " words\t" . count($combinations) . " combinations\t" . $string . "\n";
    // print_r($combinations);
}

另一种尝试:

function getCombinations2($str, $min_length = 2) {
    $words = explode(' ', $str);
    $combinations = array();
    $len = count($words);
    for ($a = $min_length; $a <= $min_length; $a++) {
        for ($pos = 0; $pos < $len; $pos ++) {
            if(($pos + $a -1) < $len) {
                $tmp = array_slice($words, $pos, $a);
                sort($tmp);
                $tmp = implode('_',$tmp);
                $combinations[] = $tmp;
            }
        }
    }
    $combinations = array_unique($combinations);
    return $combinations;
}

当您打印出组合并寻找应该存在的几个组合时,您可以知道您是成功的(例如,“fat_zoo”,“car_tan”).我的两次尝试都会展示其中的几个,但绝不是全部.

解决方法:

可能有一个更优雅的解决方案,但我用2个功能做到了.

getCombosOfLength函数为数组中的每个$intLength组合. GetCombos函数只为您想要的每个长度运行GetCombosOfLength.这非常适合生成1-5项的所有组合.如果你为所有25项组合运行它,它有一些问题.

$a = array("c", "b", "f", "v", "g", "e", "h", "i", "j", "k", "l", "m", "n", "p", "o", "r", "a", "q", "s", "t", "u", "w", "x", "y", "z");
$b = getCombos($a, 5);

print "<pre>\n";
print_r($b);

function getCombos($arrInput, $intMax = null) {
    sort($arrInput);
    if (is_null($intMax)) $intMax = count($arrInput);
    $arrOutput = array();
    for ($i = $intMax; $i > 0; $i--) {
        $arrReturn = getCombosOfLength($arrInput, $i);
        for ($j = 0; $j < count($arrReturn); $j++)  $arrOutput[] = $arrReturn[$j];
    }
    return $arrOutput;
}

function getCombosOfLength($arrInput, $intLength) {
    $arrOutput = array();
    if ($intLength == 1) {
        for ($i = 0; $i < count($arrInput); $i++) $arrOutput[] = array($arrInput[$i]);
        return $arrOutput;
    }
    $arrShift = $arrInput;
    while (count($arrShift)) {
        $x = array_shift($arrShift);
        $arrReturn = getCombosOfLength($arrShift, $intLength - 1);
        for ($i = 0; $i < count($arrReturn); $i++) {
            array_unshift($arrReturn[$i], $x);
            $arrOutput[] = $arrReturn[$i];
        }
    }
    return $arrOutput;
}
上一篇:如何在PHP中生成多个数组中的所有项目组合


下一篇:[Python]:生成所有可能组合的数组