解决循环[关闭]

I have noticed similar repetition and trying to work around using a single for loop for this if I can to minimize the code length:

I wouldn't need to use a switch case if I can form a loop instead?

  1. $returnNo variable starts at 5, each case multiplied by 2 then minus 1.

  2. where it shows "$a<=", it starts at 5 and each case multiplied by 2 then plus 3.

  3. the if() statement starting at if($matchno == 7), each case multiplied by 2 then plus 1.

  4. the final if() statement starting at if($matchno == 8), each case multiplied by 2.

  5. I have done up to case 64, it will actually go up to 512. As I know the code is repeating I am hoping someone can help me produce a single loop for this?

Many thanks!

    switch($max) {

    case 80 :
        $returnNO = 5;
        for($a = 1; $a<=5; $a++) {
            if($matchno == $a || $matchno == ($a+1)){
                $matchinfo['matchno'] = $returnNO;
                $matchinfo['place'] = ($matchno == $a ? 'clan1' : 'clan2');
                return $matchinfo;
            }
            $returnNO++;
            $a++;
        }
        if($matchno == 7){
            $matchinfo['winner'] = true;
            return $matchinfo;
        }elseif($matchno == 8){
            $matchinfo['third_winner'] = true;
            return $matchinfo;
        }
    break;

    case 160 :
        $returnNO = 9;
        for($a = 1; $a<=13; $a++) {
            if($matchno == $a || $matchno == ($a+1)){
                $matchinfo['matchno'] = $returnNO;
                $matchinfo['place'] = ($matchno == $a ? 'clan1' : 'clan2');
                return $matchinfo;
            }
            $returnNO++;
            $a++;
        }
        if($matchno == 15){
            $matchinfo['winner'] = true;
            return $matchinfo;
        }elseif($matchno == 16){ 
            $matchinfo['third_winner'] = true;
            return $matchinfo;
        }
    break;

    case 320 :
        $returnNO = 17;
        for($a = 1; $a<=29; $a++) {
            if($matchno == $a || $matchno == ($a+1)){
                $matchinfo['matchno'] = $returnNO;
                $matchinfo['place'] = ($matchno == $a ? 'clan1' : 'clan2');
                return $matchinfo;
            }
            $returnNO++;
            $a++;
        }

        if($matchno == 31){
            $matchinfo['winner'] = true;
            return $matchinfo;
        } elseif($matchno == 32){
            $matchinfo['third_winner'] = true;
            return $matchinfo;
        }
    break;
    case 640 :              
        $returnNO = 33;
        for($a = 1; $a<=61; $a++) {
            if($matchno == $a || $matchno == ($a+1)){
                $matchinfo['matchno'] = $returnNO;
                $matchinfo['place'] = ($matchno == $a ? 'clan1' : 'clan2');
                return $matchinfo;
            }
            $returnNO++;
            $a++;
        }   
        if($matchno == 63){
            $matchinfo['winner'] = true;
            return $matchinfo;
        }elseif($matchno == 64){
            $matchinfo['third_winner'] = true;
            return $matchinfo;
        }               
    break;      

    }
}

I'll use the first two cases as an example:

switch ($max) {
case 80:
        $returnNO = 5;
        $loopCount = 5;
        $winner = 7;
        $thirdWinner = 8;
        break;

    case 160:
        $returnNO = 9;
        $loopCount = 13;
        $winner = 15;
        $thirdWinner = 16;
        break;
    ...
}

for ($a = 1; $a <= $loopCount; $a++) {
    if ($matchno == $a || $matchno == ($a + 1)) {
        $matchinfo['matchno'] = $returnNO;
        $matchinfo['place'] = ($matchno == $a ? 'clan1' : 'clan2');
        return $matchinfo;
    }
}

if ($matchno == $winner) {
    $matchinfo['winner'] = true;
    return $matchinfo;
} else if ($matchno == $thirdWinner) {
    $matchinfo['third_winner'] = true;
    return $matchinfo;
}

Simply explained, remove the code that repeats all the time and try to parameterize it (either by creating a function or by putting all repeated code somewhere else... in this example, I put the repeating code after the switch statement and paremeterized it.

If I understood well, here is an alternative solution which I think would work for all cases you specified, with no use of switch case.

$div10 = $max / 10;
$maxLoop = $div10 - 3;
$returnNO = $div10 / 2 + 1;

for($a = 1; $a<=$maxLoop; $a++) {
    if($matchno == $a || $matchno == ($a+1)){
        $matchinfo['matchno'] = $returnNO;
        $matchinfo['place'] = ($matchno == $a ? 'clan1' : 'clan2');
        return $matchinfo;
    }
    $returnNO++;
    $a++;
}
if($matchno == ($div10-1)){
    $matchinfo['winner'] = true;
    return $matchinfo;
}elseif($matchno == $div10){
    $matchinfo['third_winner'] = true;
    return $matchinfo;
}