I want to compare the following two arrays:
Array
(
[0] => stdClass Object
(
[question_id] => 1
[answer_id] => 21
)
[1] => stdClass Object
(
[question_id] => 3
[answer_id] => 33
)
[2] => stdClass Object
(
[question_id] => 3
[answer_id] => 36
)
[3] => stdClass Object
(
[question_id] => 3
[answer_id] => 38
)
[4] => stdClass Object
(
[question_id] => 6
[answer_id] => 49
)
)
Array
(
[0] => stdClass Object
(
[question_id] => 3
开发者_运维技巧 [answer_id] => 38
)
[1] => stdClass Object
(
[question_id] => 3
[answer_id] => 37
)
[2] => stdClass Object
(
[question_id] => 3
[answer_id] => 33
)
[3] => stdClass Object
(
[question_id] => 1
[answer_id] => 22
)
[4] => stdClass Object
(
[question_id] => 6
[answer_id] => 49
)
)
One array is the correct answers to the assessment and the other is what the user has entered.
How can I compare these two arrays to product another which shows the question_id along with a Boolean to whether they got it right or not?
I currently experiencing the following problems:
- Some of the questions have multiple answers to them.
- the question_id orders are not the same, the correct order is the second array which isn't always in a numeric order.
Here is a solution that I hope works for you.
First, I have re-created your objects from above:
class AnswerObject {
public $question_id=0;
public $answer_id=0;
function __construct($q,$a){
$this->question_id=$q;
$this->answer_id=$a;
}
}
$obj_student_vals = array(new AnswerObject(1,21), new AnswerObject(3,33), new AnswerObject(3,36), new AnswerObject(3,38), new AnswerObject(6,49));
$obj_answer_key = array(new AnswerObject(1,22), new AnswerObject(3,33), new AnswerObject(3,37), new AnswerObject(3,38), new AnswerObject(6,49));
Now, the solution I am providing is as follows, and continues from the above code:
$flat_student_vals = array();
$flat_answer_key = array();
// flatten student vals to multi-dimensional array
// e.g. array('3'=>array(33,36,38),'6'=>array(49),'1'=>array(21))
foreach($obj_student_vals as $obj){
if(!array_key_exists($obj->question_id,$flat_student_vals))
$flat_student_vals[$obj->question_id]=array();
$flat_student_vals[$obj->question_id][]=$obj->answer_id;
}
// flatten answer key to multi-dimensional array
// e.g. array('1'=>array(22),'3'=>array(33,37,38),'6'=>array(49))
foreach($obj_answer_key as $obj){
if(!array_key_exists($obj->question_id,$flat_answer_key))
$flat_answer_key[$obj->question_id]=array();
$flat_answer_key[$obj->question_id][]=$obj->answer_id;
}
// the results for this student
$student_results = array();
// when we compare arrays between student vals and answer key,
// the order doesn't matter. the array operator `==` is only concerned
// with equality (same keys/value pairs), not identity (in same order and of same type)
foreach($flat_student_vals as $qid=>$vals){
$student_results[$qid]=($vals == $flat_answer_key[$qid]);
}
print_r($student_results);
The comments in my code are fairly self-explanatory. The bottom-line is that in order to efficiently compare student responses to the answer key, you need to flatten both of your original arrays of objects down to simple, multi-dimensional arrays.
精彩评论