skip to Main Content

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


  1. You can do something like:

    $result = [];
    
    // Loop through each competitor
    foreach ($arr2 as $competitorIndex => $competitorAnswers) {
        // Initialize a flag as correct for each competitor
        $result[$competitorIndex] = true;
    
        // Loop through each of the competitor's answers
        foreach ($competitorAnswers['answer'] as $questionIndex => $answer) {
            // If the answer is not the same as the correct answer set the result as false
            if ($answer != $arr1[$questionIndex]['answer']) {
                $result[$competitorIndex] = false;
                // No need to check further for this competitor
                break;
            }
        }
    }
    

    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.

    Login or Signup to reply.
  2. You could construct a correct response, and then compare each competitor’s entry against that:

    $correctResponse = [
        $arr1[0]['answer'],
        $arr1[1]['answer'],
    ];
    $winners = array_filter($arr2, fn($competitorResponse) =>
        $competitorResponse['answer'] === $correctResponse
    );
    

    $winners is then an array of all the competitors with the correct answers

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search