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.' ';
}
$flagvariable overwrites on eachforeachiteration. Use array to store flags$flag = 'match';to a simpleecho 'match';also do this for themismatchand loose theecho $flag;If you layout your code with sensible indentation like above, the error becomes so much easier to see