直接上代码!

$CombinList[0][0] = 'a1';
$CombinList[0][1] = 'a2';
$CombinList[1][0] = 'b4';
$CombinList[1][1] = 'b5';
$CombinList[1][2] = 'b6';
$CombinList[2][0] = 'c7';
$CombinList[2][1] = 'c8';

function combin($CombinList){
	$CombineCount = 1;
	foreach($CombinList as $Key => $Value){
		$CombineCount *= count($Value);
	}
	$RepeatTime = $CombineCount;
	foreach($CombinList as $ClassNo => $StudentList){
		$RepeatTime = $RepeatTime/count($StudentList);
		$StartPosition = 1;
		foreach($StudentList as $Student){
			$TempStartPosition = $StartPosition;
			$SpaceCount = $CombineCount/count($StudentList)/$RepeatTime;
			for($J = 1; $J <= $SpaceCount; $J ++){
				for($I = 0; $I < $RepeatTime; $I ++){
					$Result[$TempStartPosition + $I][$ClassNo] = $Student;
				}
				$TempStartPosition += $RepeatTime * count($StudentList);
			}
			$StartPosition += $RepeatTime;
		}
	}
	return $Result;
}

echo "<pre>";
$Result = combin($CombinList);
print_r($Result);
foreach($Result as $k=>$v){
	echo(implode('-', $v));
	echo($wrap);
}
echo "</pre>";

/*
结果是:

Array
(
    [1] => Array
        (
            [0] => a1
            [1] => b4
            [2] => c7
        )

    [2] => Array
        (
            [0] => a1
            [1] => b4
            [2] => c8
        )

    [3] => Array
        (
            [0] => a1
            [1] => b5
            [2] => c7
        )

    [4] => Array
        (
            [0] => a1
            [1] => b5
            [2] => c8
        )

    [5] => Array
        (
            [0] => a1
            [1] => b6
            [2] => c7
        )

    [6] => Array
        (
            [0] => a1
            [1] => b6
            [2] => c8
        )

    [7] => Array
        (
            [0] => a2
            [1] => b4
            [2] => c7
        )

    [8] => Array
        (
            [0] => a2
            [1] => b4
            [2] => c8
        )

    [9] => Array
        (
            [0] => a2
            [1] => b5
            [2] => c7
        )

    [10] => Array
        (
            [0] => a2
            [1] => b5
            [2] => c8
        )

    [11] => Array
        (
            [0] => a2
            [1] => b6
            [2] => c7
        )

    [12] => Array
        (
            [0] => a2
            [1] => b6
            [2] => c8
        )

)
a1-b4-c7
a1-b4-c8
a1-b5-c7
a1-b5-c8
a1-b6-c7
a1-b6-c8
a2-b4-c7
a2-b4-c8
a2-b5-c7
a2-b5-c8
a2-b6-c7
a2-b6-c8
*/

One Comment

Leave a Reply