这里是一个非常更新答案(最初从这个答案更新] 1)你的问题:
function findPermutations($arr, $arrLen, $size, $perArr = array(), $pos = 0, &$found = array()) {
if ($size==$pos) { //if $pos reach $size then we have found one permutation
$found[] = vsprintf("%s%s|%s%s|%s%s", $perArr);
return;
}
for ($i=0; $i<$arrLen; $i++) {
$perArr[$pos] = $arr[$i]; //put i'th char in current position
//The recursive call that move to next position with $pos+1
findPermutations($arr, $arrLen, $size, $perArr, $pos+1, $found);
}
return $found;
}
$permutations = array();
$letters = array('1','2','3');
$max_length = 6;
$permutations = findPermutations($letters, count($letters), $max_length);
for($i = 0; $i < count($permutations); $i++) {
print ($permutations[$i].'<br/>');
}
下面是我在做什么。我通过引用传入一个名为$permutations
的空数组,并且当我找到新的排列组合时,我将它们附加到它。当功能findPermutations()
完成后,我最终得到一个所有排列的数组,我可以迭代或插入。为了获得我使用的格式,vsprintf,它允许我传递一组数据并应用格式(在这种情况下为%s%s|%s%s|%s%s
)。最后,我使用默认参数值来调用这个函数更清晰。
http://stackoverflow.com/questions/5506888/permutations-all-possible-sets-of-numbers –
忘记分隔符,你可以随时添加它们。只需要选择一个随机数(1到3之间)6次,并将它们连接在一起。检查你的数据库表,看看它是否存在,如果没有添加它。运行它一堆。最终你会得到他们。 –
此外,管道分隔符只是象征性的,它对排列算法没有任何重要性,所以不要认为它是一个复杂的' –