0

I am having two arrays $exam_questions and $attempted_responses. I want to compare attempted_responses['question_id'] object to $exam_questions['id'] if they match.

output of both arrays are:

$exam_questions

Array
(
    [0] => stdClass Object
        (
            [id] => 747
            [section_id] => 
            [text] => What does HTML stand for?
            [exam_id] => 83
            [date_created] => 2019-11-27 09:41:41
            [answered] => 1
        )

    [1] => stdClass Object
        (
            [id] => 748
            [section_id] => 
            [text] => Who is making the Web standards?
            [exam_id] => 83
            [date_created] => 2019-11-27 09:43:04
            [answered] => 1
        )

    [2] => stdClass Object
        (
            [id] => 749
            [section_id] => 
            [text] => What does CSS stand for?
            [exam_id] => 83
            [date_created] => 2019-11-27 09:43:51
            [answered] => 1
        )

    [3] => stdClass Object
        (
            [id] => 750
            [section_id] => 
            [text] => Which is the correct CSS syntax?
            [exam_id] => 83
            [date_created] => 2019-11-27 09:45:17
            [answered] => 1
        )

    [4] => stdClass Object
        (
            [id] => 751
            [section_id] => 
            [text] => How do you insert a comment in a CSS file?
            [exam_id] => 83
            [date_created] => 2019-11-27 09:46:32
            [answered] => 1
        )

    [5] => stdClass Object
        (
            [id] => 752
            [section_id] => 
            [text] => What is the correct HTML for inserting an image?
            [exam_id] => 83
            [date_created] => 2019-11-27 09:47:56
            [answered] => 1
        )

    [6] => stdClass Object
        (
            [id] => 753
            [section_id] => 
            [text] => How can you make a list that lists the items with numbers?
            [exam_id] => 83
            [date_created] => 2019-11-27 09:48:50
            [answered] => 1
        )

    [7] => stdClass Object
        (
            [id] => 754
            [section_id] => 
            [text] => What is the correct HTML for creating a hyperlink?
            [exam_id] => 83
            [date_created] => 2019-11-27 09:49:45
            [answered] => 1
        )

)

$attempted_responses

Array
(
    [0] => Array
        (
            [question_id] => 747
            [answer_id] => 2641
        )

    [1] => Array
        (
            [question_id] => 748
            [answer_id] => 2646
        )

    [2] => Array
        (
            [question_id] => 749
            [answer_id] => 2650
        )

    [3] => Array
        (
            [question_id] => 750
            [answer_id] => 2654
        )

    [4] => Array
        (
            [question_id] => 751
            [answer_id] => 2656
        )

    [5] => Array
        (
            [question_id] => 752
            [answer_id] => 2661
        )

    [6] => Array
        (
            [question_id] => 753
            [answer_id] => 2663
        )

    [7] => Array
        (
            [question_id] => 754
            [answer_id] => 2668
        )
)
jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • So what is it exactly that you want to do? You can compare the values by simply checking if `attempted_responses['question_id'] === $exam_questions['id']`, but I think that is not what you are after? – MultiSuperFreek Apr 27 '20 at 13:02
  • In this am trying to match if all attempted_responses['question_id'] values match those in $exam_questions['id'] ... nearly similar to what you propose but that way its not working. – Jose Aurorah Apr 27 '20 at 13:09
  • Have you seen that `$exam_questions` is an array of objects, and `$attempted_responses` is an array of arrays? So to directly compare elements you should not do `attempted_responses['question_id'] === $exam_questions['id']` but `attempted_responses['question_id'] === $exam_questions->id`. – MultiSuperFreek Apr 27 '20 at 13:22
  • Does this answer your question? [PHP get difference of two arrays of objects](https://stackoverflow.com/questions/6472183/php-get-difference-of-two-arrays-of-objects) – Gregoire Apr 28 '20 at 10:45

1 Answers1

0
foreach($exam_questions as $exam_question){
  foreach($attempted_responses as $attempted_response){
    if($exam_question->id === $attempted_response['question_id']){
      // your code
    }
  }
}

or your convert $exam_questions as a nested array.

$exam_questions_array = json_decode(json_encode($exam_questions), true);

then it can treated as a nested array.

foreach($exam_questions_array as $exam_question){
  foreach($attempted_responses as $attempted_response){
    if($exam_question['id'] === $attempted_response['question_id']){
      // your code
    }
  }
}
Anik Anwar
  • 625
  • 5
  • 11