I have two arrays like these;
// $arr1 refers questions for a competetion.
`$arr1 = [
[0] => [
answer => answer1 // $arr1[0]['answer']: correct answer to first question
],
[1] => [
answer => answer2 // $arr1[1]['answer']: correct answer to second question
];
// $arr2 refers competitors responses to questions.
$arr2 = [
[0] => [
answer => [
[0] => response1, // first competitors response to first question(for answer1)
[1] => response2 // first competitors response to second question(for answer2)
]`your text`
],
[1] => [
answer => [
[0] => response3, // second competitors response to first question(for answer1)
[1] => response4 // second competitors response to second question(for answer2)
]
],
[2] => [
answer => [
[0] => response5, // third competitors response to first question(for answer1)
[1] => response6 // third competitors response to second question(for answer2)
]
];
….`
What i want to do is comparing competitors response with questions and find out who reponds correctly to all the questions.
I should compare answer1 (in $arr1′ 0 index) with $arr2’s response1, response3,response5 and for answer2 response2,response4,response6. And all responses should be exactly same with the correct answer.
I tried array_diff, array_intersect and since i am using Condeigniter4 also used CI array library array_deep_search, d0t_array_search but i could not figure it out. Any ideas please?
2
Answers
You can do something like:
The
$result
array will hold a boolean flag for each competitor, indicating whether they have all the correct answers. The index corresponds to each competitor’s responses in$arr2
.You could construct a correct response, and then compare each competitor’s entry against that:
$winners
is then an array of all the competitors with the correct answers