二維陣列排列組合
阿新 • • 發佈:2019-02-01
//陣列排列組合: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;
}
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;
}