有多个数组.如何计算并获得有多少种组合?
时间:2021-07-01 10:21:17
帮助过:5人阅读
有多个数组. 怎么计算并获得有多少种组合??
数组A、B、C、D
里面的个数count不一定都相同。
组合顺序是固定的 A[rand] + B[rand] + C[rand] + D[rand] = 不重复字符串
怎么获取最大的组合数量、不重复字符串??
分享到:
------解决方案--------------------本帖最后由 xuzuning 于 2013-10-11 23:59:51 编辑
数量 = count(A) * count(B) * count(C) * count(D)
$a = array('a1', 'a2');
$b = array('b1', 'b2');
$c = array('c1', 'c2', 'c3');
$d = array('d1', 'd2', 'd3');
print_r(func($a, $b, $c, $d));
function func() {
$d = func_get_args();
$r = array_shift($d);
while($d) {
$t = array();
foreach(array_shift($d) as $x)
foreach($r as $y) $t[] = $y . $x;
$r = $t;
}
return $r;
}
Array
(
[0] => a1b1c1d1
[1] => a2b1c1d1
[2] => a1b2c1d1
[3] => a2b2c1d1
[4] => a1b1c2d1
[5] => a2b1c2d1
[6] => a1b2c2d1
[7] => a2b2c2d1
[8] => a1b1c3d1
[9] => a2b1c3d1
[10] => a1b2c3d1
[11] => a2b2c3d1
[12] => a1b1c1d2
[13] => a2b1c1d2
[14] => a1b2c1d2
[15] => a2b2c1d2
[16] => a1b1c2d2
[17] => a2b1c2d2
[18] => a1b2c2d2
[19] => a2b2c2d2
[20] => a1b1c3d2
[21] => a2b1c3d2
[22] => a1b2c3d2
[23] => a2b2c3d2
[24] => a1b1c1d3
[25] => a2b1c1d3
[26] => a1b2c1d3
[27] => a2b2c1d3
[28] => a1b1c2d3
[29] => a2b1c2d3
[30] => a1b2c2d3
[31] => a2b2c2d3
[32] => a1b1c3d3
[33] => a2b1c3d3
[34] => a1b2c3d3
[35] => a2b2c3d3
)