Сравнение объектов массива, если они имеют похожие значения в PHP - PullRequest
0 голосов
/ 27 апреля 2020

У меня есть два массива $exam_questions и $attempted_responses. Я хочу сравнить объект предпринятого_ответа ['question_id'] с $ exam_questions ['id'], если они совпадают.

выходные данные обоих массивов:

$ 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
        )

)

$ попытки_ответов

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
        )
)

1 Ответ

0 голосов
/ 27 апреля 2020
foreach($exam_questions as $exam_question){
  foreach($attempted_responses as $attempted_response){
    if($exam_question->id === $attempted_response['question_id']){
      // your code
    }
  }
}

или ваш конвертированный $ exam_questions как вложенный массив.

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

, тогда он может рассматриваться как вложенный массив.

foreach($exam_questions_array as $exam_question){
  foreach($attempted_responses as $attempted_response){
    if($exam_question['id'] === $attempted_response['question_id']){
      // your code
    }
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...