假定有多个数组,每个数组选取一项,进行组合。
<?php
class args
{
var $args = array();//接受的数组参数
var $arr = array();//最终的组合结果
var $str = "";
function __construct()
{
$this->args = func_get_args();//获取传递过来的所有数组
$this->comb();
}
function comb($str="",$index=0)
{
$temp_arr = @$this->args[$index];//获取当前的数组
$index++;
if($temp_arr){
foreach($temp_arr as $v){
$this->str = $str.$v;
$this->comb($this->str,$index);
}
}else{
//file_put_contents("test.txt",$this->str."\n",FILE_APPEND);
$this->arr[] = $this->str;
$this->str="";
}
}
}
$a1 = array('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');
$a2 = array(1,2,3,4,5,6,7,8,9,0);
$a = new args($a1,$a2);
print_r($a->arr);
不确定层数的循环,首要考虑的解决办法是递归。
递归最核心的部分是理解各层变量的共用及区分。
上面代码运行的结果(美化处理后):
array(260) {
[0] => string(2) "a1"
[1] => string(2) "a2"
[2] => string(2) "a3"
[3] => string(2) "a4"
[4] => string(2) "a5"
[5] => string(2) "a6"
[6] => string(2) "a7"
[7] => string(2) "a8"
[8] => string(2) "a9"
[9] => string(2) "a0"
[10] => string(2) "b1"
[11] => string(2) "b2"
[12] => string(2) "b3"
[13] => string(2) "b4"
[14] => string(2) "b5"
[15] => string(2) "b6"
[16] => string(2) "b7"
[17] => string(2) "b8"
[18] => string(2) "b9"
[19] => string(2) "b0"
[20] => string(2) "c1"
[21] => string(2) "c2"
[22] => string(2) "c3"
[23] => string(2) "c4"
[24] => string(2) "c5"
[25] => string(2) "c6"
[26] => string(2) "c7"
[27] => string(2) "c8"
[28] => string(2) "c9"
[29] => string(2) "c0"
[30] => string(2) "d1"
[31] => string(2) "d2"
[32] => string(2) "d3"
[33] => string(2) "d4"
[34] => string(2) "d5"
[35] => string(2) "d6"
[36] => string(2) "d7"
[37] => string(2) "d8"
[38] => string(2) "d9"
[39] => string(2) "d0"
[40] => string(2) "e1"
[41] => string(2) "e2"
[42] => string(2) "e3"
[43] => string(2) "e4"
[44] => string(2) "e5"
[45] => string(2) "e6"
[46] => string(2) "e7"
[47] => string(2) "e8"
[48] => string(2) "e9"
[49] => string(2) "e0"
[50] => string(2) "f1"
[51] => string(2) "f2"
[52] => string(2) "f3"
[53] => string(2) "f4"
[54] => string(2) "f5"
[55] => string(2) "f6"
[56] => string(2) "f7"
[57] => string(2) "f8"
[58] => string(2) "f9"
[59] => string(2) "f0"
[60] => string(2) "g1"
[61] => string(2) "g2"
[62] => string(2) "g3"
[63] => string(2) "g4"
[64] => string(2) "g5"
[65] => string(2) "g6"
[66] => string(2) "g7"
[67] => string(2) "g8"
[68] => string(2) "g9"
[69] => string(2) "g0"
[70] => string(2) "h1"
[71] => string(2) "h2"
[72] => string(2) "h3"
[73] => string(2) "h4"
[74] => string(2) "h5"
[75] => string(2) "h6"
[76] => string(2) "h7"
[77] => string(2) "h8"
[78] => string(2) "h9"
[79] => string(2) "h0"
[80] => string(2) "i1"
[81] => string(2) "i2"
[82] => string(2) "i3"
[83] => string(2) "i4"
[84] => string(2) "i5"
[85] => string(2) "i6"
[86] => string(2) "i7"
[87] => string(2) "i8"
[88] => string(2) "i9"
[89] => string(2) "i0"
[90] => string(2) "j1"
[91] => string(2) "j2"
[92] => string(2) "j3"
[93] => string(2) "j4"
[94] => string(2) "j5"
[95] => string(2) "j6"
[96] => string(2) "j7"
[97] => string(2) "j8"
[98] => string(2) "j9"
[99] => string(2) "j0"
[100] => string(2) "k1"
[101] => string(2) "k2"
[102] => string(2) "k3"
[103] => string(2) "k4"
[104] => string(2) "k5"
[105] => string(2) "k6"
[106] => string(2) "k7"
[107] => string(2) "k8"
[108] => string(2) "k9"
[109] => string(2) "k0"
[110] => string(2) "l1"
[111] => string(2) "l2"
[112] => string(2) "l3"
[113] => string(2) "l4"
[114] => string(2) "l5"
[115] => string(2) "l6"
[116] => string(2) "l7"
[117] => string(2) "l8"
[118] => string(2) "l9"
[119] => string(2) "l0"
[120] => string(2) "m1"
[121] => string(2) "m2"
[122] => string(2) "m3"
[123] => string(2) "m4"
[124] => string(2) "m5"
[125] => string(2) "m6"
[126] => string(2) "m7"
[127] => string(2) "m8"
[128] => string(2) "m9"
[129] => string(2) "m0"
[130] => string(2) "n1"
[131] => string(2) "n2"
[132] => string(2) "n3"
[133] => string(2) "n4"
[134] => string(2) "n5"
[135] => string(2) "n6"
[136] => string(2) "n7"
[137] => string(2) "n8"
[138] => string(2) "n9"
[139] => string(2) "n0"
[140] => string(2) "o1"
[141] => string(2) "o2"
[142] => string(2) "o3"
[143] => string(2) "o4"
[144] => string(2) "o5"
[145] => string(2) "o6"
[146] => string(2) "o7"
[147] => string(2) "o8"
[148] => string(2) "o9"
[149] => string(2) "o0"
[150] => string(2) "p1"
[151] => string(2) "p2"
[152] => string(2) "p3"
[153] => string(2) "p4"
[154] => string(2) "p5"
[155] => string(2) "p6"
[156] => string(2) "p7"
[157] => string(2) "p8"
[158] => string(2) "p9"
[159] => string(2) "p0"
[160] => string(2) "q1"
[161] => string(2) "q2"
[162] => string(2) "q3"
[163] => string(2) "q4"
[164] => string(2) "q5"
[165] => string(2) "q6"
[166] => string(2) "q7"
[167] => string(2) "q8"
[168] => string(2) "q9"
[169] => string(2) "q0"
[170] => string(2) "r1"
[171] => string(2) "r2"
[172] => string(2) "r3"
[173] => string(2) "r4"
[174] => string(2) "r5"
[175] => string(2) "r6"
[176] => string(2) "r7"
[177] => string(2) "r8"
[178] => string(2) "r9"
[179] => string(2) "r0"
[180] => string(2) "s1"
[181] => string(2) "s2"
[182] => string(2) "s3"
[183] => string(2) "s4"
[184] => string(2) "s5"
[185] => string(2) "s6"
[186] => string(2) "s7"
[187] => string(2) "s8"
[188] => string(2) "s9"
[189] => string(2) "s0"
[190] => string(2) "t1"
[191] => string(2) "t2"
[192] => string(2) "t3"
[193] => string(2) "t4"
[194] => string(2) "t5"
[195] => string(2) "t6"
[196] => string(2) "t7"
[197] => string(2) "t8"
[198] => string(2) "t9"
[199] => string(2) "t0"
[200] => string(2) "u1"
[201] => string(2) "u2"
[202] => string(2) "u3"
[203] => string(2) "u4"
[204] => string(2) "u5"
[205] => string(2) "u6"
[206] => string(2) "u7"
[207] => string(2) "u8"
[208] => string(2) "u9"
[209] => string(2) "u0"
[210] => string(2) "v1"
[211] => string(2) "v2"
[212] => string(2) "v3"
[213] => string(2) "v4"
[214] => string(2) "v5"
[215] => string(2) "v6"
[216] => string(2) "v7"
[217] => string(2) "v8"
[218] => string(2) "v9"
[219] => string(2) "v0"
[220] => string(2) "w1"
[221] => string(2) "w2"
[222] => string(2) "w3"
[223] => string(2) "w4"
[224] => string(2) "w5"
[225] => string(2) "w6"
[226] => string(2) "w7"
[227] => string(2) "w8"
[228] => string(2) "w9"
[229] => string(2) "w0"
[230] => string(2) "x1"
[231] => string(2) "x2"
[232] => string(2) "x3"
[233] => string(2) "x4"
[234] => string(2) "x5"
[235] => string(2) "x6"
[236] => string(2) "x7"
[237] => string(2) "x8"
[238] => string(2) "x9"
[239] => string(2) "x0"
[240] => string(2) "y1"
[241] => string(2) "y2"
[242] => string(2) "y3"
[243] => string(2) "y4"
[244] => string(2) "y5"
[245] => string(2) "y6"
[246] => string(2) "y7"
[247] => string(2) "y8"
[248] => string(2) "y9"
[249] => string(2) "y0"
[250] => string(2) "z1"
[251] => string(2) "z2"
[252] => string(2) "z3"
[253] => string(2) "z4"
[254] => string(2) "z5"
[255] => string(2) "z6"
[256] => string(2) "z7"
[257] => string(2) "z8"
[258] => string(2) "z9"
[259] => string(2) "z0"
}