如何在 php 中比较 2 个数组的值并输出差异
how to compare the values of 2 arrays with each other in php and output the differences
我有 2 个数组,每个数组有 10 个值。
var $correct answers
的 print_r 给我这个输出:
Array (
[0] => 3
[1] => 0
[2] => 2
[3] => 3
[4] => 2
[5] => 2
[6] => 3
[7] => 1
[8] => 3
[9] => 1
)
var $choosen_answers
的 print_r 给我这个输出:
Array (
[0] => 2 // different value
[1] => 0
[2] => 1 // different value
[3] => 3
[4] => 2
[5] => 2
[6] => 3
[7] => 1
[8] => 3
[9] => 0 // different value
)
数组中的每个值对应问题的编号,所以
[0] has the value of question 1
[1] has the value of question 2
[2] has the value of question 3
and so on...
我想要实现的目标:将这些数组相互比较并给出如下输出:
Number of wrong answers: 3
Wrong answers:
Question 1
Question 3
Question 10
我怎样才能做到这一点?
function get_wrong_answers($correct_answers, $chosen_answers) {
// For all given answers,
foreach( $chosen_answers as $answer_number=>$given_single_answer ) {
// check if the answer is correct
if( $correct_answers[$answer_number] === $given_single_answer )
// If its correct, remove it from the chosen answers
unset( $chosen_answers[$key] );
}
return $chosen_answers;
}
$wrong_answers = get_wrong_answers($a,$b);
echo 'Number of wrong answers: ' . count( $wrong_answers );
echo 'Wrong answers:';
foreach( $wrong_answers as $answer_number=>$answer ) {
echo 'Question ' . $answer_number;
}
尊重上面的答案,但可以更容易地完成:
https://www.php.net/manual/en/function.array-diff-assoc.php
$compare_values = array_diff_assoc($correct_answers, $chosen_answers) ; // compare each value of the 2 arrays with each other
echo 'Wrong answers: '.count($compare_values).'<br />';
echo 'Wrong questions:<br />';
foreach($compare_values as $key => $value) {
echo 'Question: '.($key + 1).'<br />'; // + 1 because question 0 does not exist
}
我有 2 个数组,每个数组有 10 个值。
var$correct answers
的 print_r 给我这个输出:
Array (
[0] => 3
[1] => 0
[2] => 2
[3] => 3
[4] => 2
[5] => 2
[6] => 3
[7] => 1
[8] => 3
[9] => 1
)
var $choosen_answers
的 print_r 给我这个输出:
Array (
[0] => 2 // different value
[1] => 0
[2] => 1 // different value
[3] => 3
[4] => 2
[5] => 2
[6] => 3
[7] => 1
[8] => 3
[9] => 0 // different value
)
数组中的每个值对应问题的编号,所以
[0] has the value of question 1
[1] has the value of question 2
[2] has the value of question 3
and so on...
我想要实现的目标:将这些数组相互比较并给出如下输出:
Number of wrong answers: 3
Wrong answers:
Question 1
Question 3
Question 10
我怎样才能做到这一点?
function get_wrong_answers($correct_answers, $chosen_answers) {
// For all given answers,
foreach( $chosen_answers as $answer_number=>$given_single_answer ) {
// check if the answer is correct
if( $correct_answers[$answer_number] === $given_single_answer )
// If its correct, remove it from the chosen answers
unset( $chosen_answers[$key] );
}
return $chosen_answers;
}
$wrong_answers = get_wrong_answers($a,$b);
echo 'Number of wrong answers: ' . count( $wrong_answers );
echo 'Wrong answers:';
foreach( $wrong_answers as $answer_number=>$answer ) {
echo 'Question ' . $answer_number;
}
尊重上面的答案,但可以更容易地完成: https://www.php.net/manual/en/function.array-diff-assoc.php
$compare_values = array_diff_assoc($correct_answers, $chosen_answers) ; // compare each value of the 2 arrays with each other
echo 'Wrong answers: '.count($compare_values).'<br />';
echo 'Wrong questions:<br />';
foreach($compare_values as $key => $value) {
echo 'Question: '.($key + 1).'<br />'; // + 1 because question 0 does not exist
}