简体   繁体   中英

nested foreach loop, with php and laravel

I am having problem with my nested foreach loop, I have this code of my controller retrieving the values of two columns in separate table in my database.

What I want is to compare each values against the other table vice versa,...

table1             table2
some column1       some column2 
 a                  b
 b                  b
 c                  c

My desired output would be if the values of two columns compare, if it is true then output "match" otherwise "mismatch".

Here the attempt, but it doesn't work, only the last item in both tables column are being compared. i think i am missing with my nested loops.

///snippet///

controller

$temp_answers = array();
$answers = array();

$temp_answers = Tempanswer::where('subject_id', $subject_id)
                             ->where('student_id', $student_id)
                             ->lists('temp_answer');

$answers = Question::where('subject_slug', $subject->slug)
                             ->lists('letteranswer');

foreach ($temp_answers as $temp_answer) {

    foreach ($answers as $answer) {
        if($answer == $temp_answer){
            $flag = 'match';
        }else 
            $flag = 'mismatch';
    }
    echo $flag.' ';

 }

CMIIW. You want to check table1 and table2 is match. So if its not match you will get message "mismatch"

Assumption 1 : if each data from table1 is compared with all data in table2.

foreach ($temp_answers as $temp_answer) {

    foreach ($answers as $answer) {
        if($answer == $temp_answer){
            $flag = 'match';
        }else {
            $flag = 'mismatch';
            break;
        }
    }

    if($flag == 'mismatch'){
        break;
    }  
 }

echo $flag;

Assumption 2 : Each table is compared by each row. I mean row1 table1 is compared with row1 table2 and than row2 table1 is compared with row2 table2.

$flag='';
foreach ($temp_answers as $key1=>$temp_answer) {
    foreach ($answers as $key2=>$answer) {
        if($key1 == $key2){
            if($answer == $temp_answer){
                $flag = $flag.'match ';
            }else {
                $flag = $flag.'mismatch ';
            }
            break;
        }
    }
}
echo $flag;

Well I would have done it like this.

First give variables names that help you know what is in them

Second always use {} in if else even if there is only one line in the if or else , it makes it easier to see where things actually start and finish.

Then always use indentation as well so you can visually see where a block of code starts and finishes. Remember, you may have to come back to a piece of code weeks after writing it, so make it easy to read as it may be you that has to work out what you did.

Also when you output info about what matches or mismatches also include the data you need to see in order to tell that the matching is correct. Then if it does not do quite what you want you can easily see whats going wrong.

$temp_answers = array();
$questions = array();

$temp_answers = Tempanswer::where('subject_id', $subject_id)
                             ->where('student_id', $student_id)
                             ->lists('temp_answer');

$questions = Question::where('subject_slug', $subject->slug)
                             ->lists('letteranswer');

foreach ($temp_answers as $temp_answer) {

    foreach ($questions as $question ) {

        if($question == $temp_answer){
            echo "Question = $question TempAnswer = $temp_answer > MATCH" . PHP_EOL;
        } else {
            echo "Question = $question TempAnswer = $temp_answer > MISMATCH" . PHP_EOL;
        }
    }

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM