將二維數組排列組合

public function main()
{
    $list=[
        [7,1,3],//第一位
        [5,],//第二位
        [1],//第三位
        [3,5],//第四位
        [4,6]//第五位
    ];
    $res=$this->pailie($list);
    dump($res);
}
public function pailie ($CombinList){
    /* 計算C(a,1) * C(b, 1) * ... * C(n, 1)的值(計算排列組合總次數) */
    $CombineCount = 1;
    foreach ($CombinList as $Key => $Value) {
        $CombineCount *= count($Value);
    }
    $RepeatTime = $CombineCount;
    foreach ($CombinList as $ClassNo => $StudentList) {
        // $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;
        }
    }
    /* 返回結果 */
    $result=[];
    foreach($Result as $value){
        $result[]=implode(',',$value);
    }
    return $result;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章