//基于php实现数组排列组合:array(array(1,2,3),array('a','b','c','d'),array('白色','黑色'))
function combination_arr($arrs) {
$num = 1;
foreach ($arrs as $k=>$v) {
$num *= count($v);
}
$arr_num = $num;
foreach ($arrs as $key=>$v_list) {
$v_num = count($v_list);
//$v_list中的元素在排列组合中出现的最大重复次数
$arr_num = $arr_num / $v_num;
$position = 0;
// 开始对$v_list进行循环
foreach($v_list as $v)
{
$v_position = $position;
$count = $num / $v_num / $arr_num;
for($j = 1; $j <= $count; $j ++)
{
for($i = 0; $i < $arr_num; $i ++)
{
$result[$v_position + $i][$key] = $v;
}
$v_position += $arr_num * $v_num;
}
$position += $arr_num;
}
}
return $result;
}
二维数组排列组合
于 2016-12-05 09:52:09 首次发布