Question

$condition1[0] = 2;
$condition1[1] = 3;
$condition2[0] = 3;
$condition2[1] = 2;
$condition2[2] = 1;

for($i=0; $i<3; $i++){
    for($j=0; $j<2;$j++){
        if($condition1[$j] == $condition2[$i]){
            $permission = false;
            continue;
        }
    }
    if($permission){
        echo 'success';
    }
}

As you can see I want to check two arrays. "success" must be echoed when there's a different value in $condition2 in this case there's only one difference which is $condition2[2] = 1 so "success" must be echoed only once, but it happens twice!

and also if I use continue; like the example above, does it skip the whole inner for()?

Était-ce utile?

La solution

if all what you care about is finding one difference the below code should do the trick:

$permission = false;
for($i=0; $i<3; $i++){
    for($j=0; $j<2;$j++){
        if($condition1[$j] != $condition2[$i]){
            $permission = true;
            break;
        }
    }
} 

    if($permission){
        echo 'success';
    }

Autres conseils

First as Barmar points out, you might want to use break and not continue.

Second, in your code, $permission is never set to true. So, it is not surprising that it does not echo 'success'.

Maybe what you want is this (althgout i must admit I am not sure to know what you want to achieve):

$condition1[0] = 2;
$condition1[1] = 3;
$condition2[0] = 3;
$condition2[1] = 2;
$condition2[2] = 1;

for($i=0; $i<3; $i++){
    $permission = true;
    for($j=0; $j<2;$j++){
        if($condition1[$j] == $condition2[$i]){
            $permission = false;
            break;
        }
    }
    if($permission){
        echo 'success';
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top