如果在PHP中具有相似的值,则比较数组对象

问题描述 投票:0回答:1

我有两个数组$exam_questions$attempted_responses。如果它们匹配,我想将tryped_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
        )

)

$ 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
        )
)
php
1个回答
0
投票
foreach($exam_questions as $exam_question){
  foreach($attempted_responses as $attempted_response){
    if($exam_question->id === $attempted_response['question_id']){
      // your code
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.