循环遍历多个数组递增字符串的长度

时间:2012-01-28 16:59:35

标签: php arrays

我有

$char=array("1","2","3","4","5","6","7","8","9","0","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","-");
$doma=array("aero","asia","biz","cat","com","coop","info","int","jobs","mobi","museum","name","net","org","pro","tel","travel","xxx","edu","gov","mil","co.uk","co.nr","co.au","au","ca","co.cc","cc","co","cn","co.jp","de","es","ie","in","it","jp","nl","nz","ru","co.tk","tk","tv","us")

我想做的是:

从长度为1到长度为32的

将字符排列成一个字符串,回显字符串,然后再次返回到开头。所以最终我的浏览器看起来像这样:

0.aero
1.aero
2.aero
3.aero
....
x.aero
y.aero
z.aero
-.aero
00.aero
01.aero
02.aero
....
za.aero
zb.aero
zc.aero
zd.aero
....
50x90zx908.aero
50x90zx909.aero
50x90zx90a.aero
50x90zx90b.aero
....
50x90zx910.aero
50x90zx911.aero
ect; ect;

我如何创建for循环来执行此操作?包括$ doma到最后以及每个循环?

我知道这是巨大的,但是当我有了一个想法时,我得尝试一下;)

2 个答案:

答案 0 :(得分:1)

如果您真的想要使用for循环执行此操作,请创建其中的33个,每个所需长度(1-32)一个,$doma数组一个。

但我不这样做。相反,请注意$char数组中所需的字符组合实际上形成了一个树。根节点表示空字符串。根的每个子节点表示1个字符的组合(“1”,“2”,“3”,......)。这些节点的每个子节点表示具有其父节点前缀的2字符组合(因此“1”节点的子节点都将以“1”开头),依此类推。树的叶子将是$char中所有字符的32个字符组合。如果只有三个字符a,b和c,它看起来像这样:

Tree example

然后你可以创建一个递归函数来实现这种树的深度优先遍历,而不是在内存中生成树然后打印它,你可以让函数在到达时输出每个节点的内容它。抛出一个参数,允许你在节点内容之后放置一个后缀,并将函数包装在循环中,遍历$doma中的所有元素,然后你就完成了。

function f($array, $limit, $suffix, $prefix = "", $counter = 0)
{
    if ($counter > 0) {
        print "$prefix.$suffix\n";
    }

    if ($counter < $limit) {
        foreach ($array as $element) {
            f($array, $limit, $suffix, $prefix . $element, $counter+1);
        }
    }
}

$char=array("1","2","3","4","5","6","7","8","9","0","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","-");
$doma=array("aero","asia","biz","cat","com","coop","info","int","jobs","mobi","museum","name","net","org","pro","tel","travel","xxx","edu","gov","mil","co.uk","co.nr","co.au","au","ca","co.cc","cc","co","cn","co.jp","de","es","ie","in","it","jp","nl","nz","ru","co.tk","tk","tv","us");

foreach ($doma as $d) {
    f($char, 32, $d);
}

顺序与您指定的顺序不完全相同,但此顺序在逻辑上与数组中元素的顺序和深度优先遍历顺序一致。

答案 1 :(得分:-1)

代码基本上将字符组合视为base-35数字。它每个循环增加一个。

<?php

    $char=array("1","2","3","4","5","6","7","8","9","0","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","-");
    $doma=array("aero","asia","biz","cat","com","coop","info","int","jobs","mobi","museum","name","net","org","pro","tel","travel","xxx","edu","gov","mil","co.uk","co.nr","co.au","au","ca","co.cc","cc","co","cn","co.jp","de","es","ie","in","it","jp","nl","nz","ru","co.tk","tk","tv","us");

    $combo = array(0,0,0,0,0,0,0,0,0,0); //this stores the ten "digits" of the characters
    $pval = 9; //this is the active "place value", the one that you increment
    $list = "";

    for($i=0;$i<44;$i++) { //loops through doma
        for($j=0;$j<2758547353515625;$j++) { //loops through character combos, that long number is the permutations

            for($l=0;$l<10;$l++) { //loop displays combo
                $list .= $char[$combo[$l]];
            }
            $list .= "." . $doma[$i] . "<br/>"; //add combo to list

            /*This next part check to see if the active digit is 35. It is is, it sets it 
            equal to zero and looks to the digit on the left. It repeats this until it
            no longer finds a 35. This is like going from 09 to 10 in the decimal system. */
            if($combo[$pval] == 35) { 
                while($combo[$pval] == 35) {
                    $combo[$pval] = 0;
                    $pval--;
                }
            }
            $combo[$pval]++; //whatever digit it left on gets incremented.



            $pval = 9; //reset, go back to first place value
        }
        for($l=0;$l<10;$l++) {
            $combo[$l] = 0; //reset combo for the next top level domain
        }
    }
    echo $list; //print the compiled list.
    ?>