【问题标题】:comparing two arrays to find results of assessment比较两个数组以找到评估结果
【发布时间】:2012-12-21 04:16:54
【问题描述】:

我想比较以下两个数组:

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
        )

)

一个数组是评估的正确答案,另一个是用户输入的内容。

我如何将这两个数组与另一个显示 question_id 以及布尔值的产品进行比较,以确定它们是否正确?

我目前遇到以下问题:

  • 有些问题有多个答案。
  • question_id 顺序不一样,正确的顺序是第二个数组,它并不总是按数字顺序。

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    这是一个我希望对你有用的解决方案。

    首先,我从上面重新创建了您的对象:

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

    现在,我提供的解决方案如下,并继续上面的代码:

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

    我的代码中的 cmets 是不言自明的。最重要的是,为了有效地比较学生对答案键的回答,您需要将两个原始对象数组扁平化为简单的多维数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      相关资源
      最近更新 更多