简体   繁体   English

嵌套的foreach循环,带有php和laravel

[英]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. 我的嵌套foreach循环有问题,我的控制器代码检索了数据库中单独表中两列的值。

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". 我想要的输出将是两列的值是否比较,如果为true,则输出“ match”,否则输出“ 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. CMIIW。 You want to check table1 and table2 is match. 您要检查table1和table2是否匹配。 So if its not match you will get message "mismatch" 因此,如果不匹配,您将收到消息“不匹配”

Assumption 1 : if each data from table1 is compared with all data in table2. 假设1 :是否将表1中的每个数据与表2中的所有数据进行比较。

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. 假设2 :每个表都按行进行比较。 I mean row1 table1 is compared with row1 table2 and than row2 table1 is compared with row2 table2. 我的意思是将row1 table1与row1 table2比较,然后将row2 table1与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. 其次总是使用{}if else即使只有一个行if还是else ,这使得它更容易看到的东西居然开始和结束。

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;
        }
    }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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