Aaron Bailey
Aaron Bailey

Reputation: 55

Foreach if and else problems PHP

I am having a few problems with php when I use a foreach() for 10 arrays and use the if(variable = something){}else{} to show what i want from it.

The else returns everything 10x.

What I need help with is when 6111 or 6112 does not exists it shows only once what i have in the else{} instead of showing it 10x since there is 10 arrays.

foreach(){
    if($statplayemasteriesslotsID == 6111){
         //dothis
    } else if($statplayemasteriesslotsID == 6112){
         //dothis
    } else {
        //dothis
    }
}

Is there anyway to do this?

I also tried doing this, with the if{} else {} out side the foreach but it instead of returning everything multiple time it mixes them all up in each other.

foreach(){
    if($statplayemasteriesslotsID == 6111){
        $statplayemasteriesslotsID1 = 6111;
    } else if($statplayemasteriesslotsID == 6112){
        $statplayemasteriesslotsID1 = 6112;
    } 
}


if($statplayemasteriesslotsID1 == 6111){
    //dothis
} else if($statplayemasteriesslotsID1 == 6112){
   //dothis
} else {
   //dothis
}

I currently am using the last way i done it if anyone knows away to stop this please let me know. thanks in advance

Upvotes: 1

Views: 63

Answers (1)

Jeff
Jeff

Reputation: 6953

I'd do this:

$doC = false;

foreach(){
    if($statplayemasteriesslotsID == 6111){
        //dothis
    } else if($statplayemasteriesslotsID == 6112){
        //dothis
    } else {
       //don't do it yet, just set a flag:
       $doC = true;
    }
}

if($doC) {
    // do it once only!
}

Upvotes: 1

Related Questions