Clemen Swichmann
Clemen Swichmann

Reputation: 83

how to set flag if certain condition met in php loop

I've two arrays. I am looping first array and in that loop i am checking array_diff with two array. If there is difference in array found it should return flag as 1 else it should return as 0. I've tried breaking out of loop but that also not working. Code given below:-

$flg = 0;
foreach ($rest as $key => $value) {
    $arr1 = $_POST['attrib_singname'];
    $arr2 = $rest[$key];
    $result = array_diff($arr1, $arr2);
    if(empty($result)){
       $flg = 1; //Same Array
       break 1;
    }else{
        $flg = 0; //Diff Array
        break 1;
    }
}

if the diff is found in first step then it is returning 1 but if it is not in first step then every time it is returning 0.

Upvotes: 2

Views: 6751

Answers (1)

Silas T
Silas T

Reputation: 36

You should replace the if/else statement with only an if statement that sets the variable to 1 if a change is found. Then, outside of your foreach loop, check if the variable is 1 (different array) or 0 (same array).

Upvotes: 1

Related Questions