时间:2021-07-01 10:21:17 帮助过:4人阅读
$d = str_split('abcd');
print_r(array_map('join', f(4, $d)));
function f($n, $d) {
$t = array_chunk($d, 1);
if($n > 2) $t = f($n-1, $d);
foreach($t as $x)
foreach(array_chunk($d, 1) as $y) {
if(end($x) != end($y)) $r[] = array_merge($x, $y);
}
return $r;
}
Array
(
[0] => abab
[1] => abac
[2] => abad
[3] => abca
[4] => abcb
[5] => abcd
[6] => abda
[7] => abdb
[8] => abdc
[9] => acab
[10] => acac
[11] => acad
[12] => acba
[13] => acbc
[14] => acbd
[15] => acda
[16] => acdb
[17] => acdc
[18] => adab
[19] => adac
[20] => adad
[21] => adba
[22] => adbc
[23] => adbd
[24] => adca
[25] => adcb
[26] => adcd
[27] => baba
[28] => babc
[29] => babd
[30] => baca
[31] => bacb
[32] => bacd
[33] => bada
[34] => badb
[35] => badc
[36] => bcab
[37] => bcac
[38] => bcad
[39] => bcba
[40] => bcbc
[41] => bcbd
[42] => bcda
[43] => bcdb
[44] => bcdc
[45] => bdab
[46] => bdac
[47] => bdad
[48] => bdba
[49] => bdbc
[50] => bdbd
[51] => bdca
[52] => bdcb
[53] => bdcd
[54] => caba
[55] => cabc
[56] => cabd
[57] => caca
[58] => cacb
[59] => cacd
[60] => cada
[61] => cadb
[62] => cadc
[63] => cbab
[64] => cbac
[65] => cbad
[66] => cbca
[67] => cbcb
[68] => cbcd
[69] => cbda
[70] => cbdb
[71] => cbdc
[72] => cdab
[73] => cdac
[74] => cdad
[75] => cdba
[76] => cdbc
[77] => cdbd
[78] => cdca
[79] => cdcb
[80] => cdcd
[81] => daba
[82] => dabc
[83] => dabd
[84] => daca
[85] => dacb
[86] => dacd
[87] => dada
[88] => dadb
[89] => dadc
[90] => dbab
[91] => dbac
[92] => dbad
[93] => dbca
[94] => dbcb
[95] => dbcd
[96] => dbda
[97] => dbdb
[98] => dbdc
[99] => dcab
[100] => dcac
[101] => dcad
[102] => dcba
[103] => dcbc
[104] => dcbd
[105] => dcda
[106] => dcdb
[107] => dcdc
)