PHP提供了4char A-Z、A-Z、0-9的所有可能变体
我必须列出所有可能的4个字符a-Z、a-Z、0-9的排列以及所有这些字符的组合。我如何通过所有可能的组合并打印它们?PHP提供了4char A-Z、A-Z、0-9的所有可能变体,php,recursion,permutation,Php,Recursion,Permutation,我必须列出所有可能的4个字符a-Z、a-Z、0-9的排列以及所有这些字符的组合。我如何通过所有可能的组合并打印它们? 它的用途:我需要把它做成一个html文档,然后打印出来,作为我们大学的随机唯一用户名,这样学生们就可以根据一个唯一id提供反馈,这个id在使用时将失效。我不能把这个程序改成更好的 类似的事?(伪代码) $a=array_merge(范围('a','Z')、范围('a','Z')、范围(0,9)); foreach($a作为$key=>$b){ 回显$b.$a[$key+1]。$
它的用途:我需要把它做成一个html文档,然后打印出来,作为我们大学的随机唯一用户名,这样学生们就可以根据一个唯一id提供反馈,这个id在使用时将失效。我不能把这个程序改成更好的 类似的事?(伪代码)
$a=array_merge(范围('a','Z')、范围('a','Z')、范围(0,9));
foreach($a作为$key=>$b){
回显$b.$a[$key+1]。$a[$key+2]。$a[$key+3]。
;
}
$numbers=范围(0,9);
$lowerCaseLetters=范围('a','z');
$upperCaseLetters=范围('A','Z');
foreach($number作为$number){
foreach($lowerCaseLetters作为$lowerCaseLetter){
foreach($uperCaseLetters作为$upercaseLetter){
回显$number.$lowerCaseLetter.$upperCaseLetter.“
”;
回显$number.$upperCaseLetter.$lowerCaseLetter.“
”;
回显$lowerCaseLetter.$number.$upperCaseLetter.“
”;
回显$lowerCaseLetter.$upperCaseLetter.$number.“
”;
回显$upperCaseLetter.$lowerCaseLetter.$number.“
”;
回显$upperCaseLetter.$number.$lowerCaseLetter.“
”;
}
}
}
有点匆忙,但:
class Combinations{
protected $characters = array();
protected $combinations = array();
public function __construct($characters){
$this->characters = $characters;
}
public function getCombinations($length){
foreach($this->characters as $comb)
{
$this->getRecurse($comb, $length - 1);
}
$combinations = $this->combinations;
$this->combinations = array();
return $combinations;
}
public function getRecurse($combination, $length){
if($length <= 0){
$this->combinations[] = $combination;
}else{
foreach($this->characters as $comb)
{
$this->getRecurse($combination.$comb, $length - 1);
}
}
}
}
$characters = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));
$comb = new Combinations($characters);
print_r( $comb->getCombinations(4) );
类组合{
受保护的$characters=array();
受保护的$combines=array();
公共函数构造($characters){
$this->characters=$characters;
}
公共函数getCombinations($length){
foreach($this->characters as$comb)
{
$this->getRecurse($comb,$length-1);
}
$combinations=$this->combinations;
$this->combines=array();
返回$组合;
}
公共函数getRecurse($combination,$length){
如果($长度组合[]=$组合;
}否则{
foreach($this->characters as$comb)
{
$this->getRecurse($combination.$comb,$length-1);
}
}
}
}
$characters=array_merge(范围('A','Z')、范围('A','Z')、范围(0,9));
$comb=新组合($characters);
打印($comb->getcompositions(4));
传递给getcombines
的数字是您希望组合的长度。使用一种非常非传统的方法,您可以使用dec2any,如下所示:
$index = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$base = strlen($index);
$len = 4;
for ($i = 0, $l = pow(strlen($index), $len); $i < $l; $i++) {
echo str_pad(dec2any($i, $base, $index), $len, "0", STR_PAD_LEFT), "\n";
}
function dec2any( $num, $base=62, $index=false ) {
if (! $base ) {
$base = strlen( $index );
} else if (! $index ) {
$index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base );
}
$out = "";
for ( $t = floor( log10( $num ) / log10( $base ) ); $t >= 0; $t-- ) {
$a = floor( $num / pow( $base, $t ) );
$out = $out . substr( $index, $a, 1 );
$num = $num - ( $a * pow( $base, $t ) );
}
return $out;
}
$index=“0123456789abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz”;
$base=strlen($index);
$len=4;
对于($i=0,$l=pow(strlen($index),$len);$i<$l;$i++){
echo str_pad(dec2any($i,$base,$index),$len,“0”,str_pad_LEFT),“\n”;
}
函数dec2any($num,$base=62,$index=false){
如果(!$base){
$base=strlen($index);
}else if(!$index){
$index=substr(“0123456789abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz”,0,$base);
}
$out=“”;
对于($t=floor(log10($num)/log10($base));$t>=0;$t--){
$a=地板($num/pow($base,$t));
$out=$out.substr($index,$a,1);
$num=$num-($a*pow($base,$t));
}
退回$out;
}
通过更改$index和$len,可以很容易地对其进行调整。警告:这需要一些时间来计算,因为有62^4=14776336个可能的组合。如果累积结果而不直接打印它们,也需要大量内存
function print_combinations($characters, $length, $combination = '') {
if ($length > 0) {
foreach ($characters as $i) {
print_combinations($characters, $length - 1, $combination . $i);
}
} else {
printf("%s\n", $combination);
}
}
$characters = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));
print_combinations($characters, 4);
使用分隔符的更通用方法:
function getCombinations($base,$delimiter="|"){
$base = array_values($base);
$baselen = count($base);
if($baselen == 0){
return;
}
if($baselen == 1){
return $base[0];
}else{
//get one level lower combinations
$oneLevelLowerArray = $base;
$orig_part = $oneLevelLowerArray[0];
unset($oneLevelLowerArray[0]);
$oneLevelLower = getCombinations($oneLevelLowerArray,$delimiter);
$countOLL = count($oneLevelLower);
foreach ($orig_part as $rowa) {
foreach ($oneLevelLower as $rowb) {
$resultArray[] = $rowa.$delimiter.$rowb;
}
}
}
return $resultArray;
}
正面提示:这会生成只有三个字符长的代码。是的,我需要它从1个字符变为4个字符。正面提示:这只会生成少量代码并导致错误。你对数组合并的想法是正确的,但是如果你想输出所有可能的组合,你需要四个foreach
。@Pinkothic,我是不需要四个foreach,我将一个输出的四个字符分别移位一个字符。(+1、+2、+2)是的,但OP需要所有的组合。也就是说,不只是ABCD、BCDE、CDEF等等,还有AAAA、AAAB、AAAC、AAAD等等。你当前的代码没有提供这些。我也会搜索置换,递归函数总是让我感觉模糊,但这是最好的答案。如何将此函数与开始索引一起使用?例如,生成po长度从4到6的可能组合
function print_combinations($characters, $length, $combination = '') {
if ($length > 0) {
foreach ($characters as $i) {
print_combinations($characters, $length - 1, $combination . $i);
}
} else {
printf("%s\n", $combination);
}
}
$characters = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));
print_combinations($characters, 4);
function getCombinations($base,$delimiter="|"){
$base = array_values($base);
$baselen = count($base);
if($baselen == 0){
return;
}
if($baselen == 1){
return $base[0];
}else{
//get one level lower combinations
$oneLevelLowerArray = $base;
$orig_part = $oneLevelLowerArray[0];
unset($oneLevelLowerArray[0]);
$oneLevelLower = getCombinations($oneLevelLowerArray,$delimiter);
$countOLL = count($oneLevelLower);
foreach ($orig_part as $rowa) {
foreach ($oneLevelLower as $rowb) {
$resultArray[] = $rowa.$delimiter.$rowb;
}
}
}
return $resultArray;
}