2011-05-15 5 views
1

의 결과를 찾기 위해 나는 다음과 같은 두 배열을 비교하려면 :두 배열을 비교하는 평가

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 순서가 동일하지 않은 경우 올바른 순서는 항상 숫자 순서가 아닌 두 번째 배열입니다.

답변

2

다음은 내가 원하는 해결책입니다.

첫째, 나는 위에서 개체를 다시 만들었습니다

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

코멘트 내 코드에서 꽤 자명하다. 결론은 학생의 답을 해답 키와 효율적으로 비교하기 위해 원본 배열을 모두 단순한 다차원 배열로 전개해야한다는 것입니다.

관련 문제