Reputation: 4497
I'm having these arrays, which the 1st one represents what answers has given a user on a questionnaire and the 2nd one the correct answers of each quetionnaire:
$given_answers => array(3) {
[46] => string(2) "82"
[47] => string(2) "86"
[48] => array(2) {
[0] => string(2) "88" // key is not questionID here
[1] => string(2) "89" // key is not questionID here
}
}
$correct_answers => array(3) {
[46] => int(84)
[47] => int(86)
[48] => array(2) {
[0] => int(88) // key is not questionID here
[1] => int(91) // key is not questionID here
}
}
NOTE: each key in both arrays represent the questionID, except from these I mention in comments. So for example questionID 46 has answerID 84 as correct answer and questionID 48 has as correct answers both 88 and 91, so the keys 0, 1 are simple array indexes in this case.
What I'm trying to do is compare both arrays and check if the answers
(questionID) match for each questionID. How can I do this? I tried using array_diff()
but I'm getting an error
$result = array_diff($correct_answers, $given_answers);
Severity: Notice
Message: Array to string conversion
Upvotes: 2
Views: 1227
Reputation: 47991
Recursion is not necessary, because simple strings and subarrays can be evaluated by array_udiff_assoc()
. Subarrays with disordered elements will be deemed different, but subarray values which are loosely equal will be deemed the same. Of course, if the result array is empty, then the arrays are deemed "equal". Demo
$given_answers = [
46 => "82",
47 => "86",
48 => ["88", "89"],
49 => "83",
50 => ["51", "49"],
51 => ["1", "2", "3"],
];
$correct_answers = [
46 => "84",
50 => ["49", "51"],
47 => "86",
49 => "86",
48 => ["88", "91"],
51 => ["1", 2, "3"],
];
var_export(
array_udiff_assoc(
$correct_answers,
$given_answers,
fn($a, $b) => $a <=> $b
)
);
Output:
array (
46 => '84',
50 =>
array (
0 => '49',
1 => '51',
),
49 => '86',
48 =>
array (
0 => '88',
1 => '91',
),
)
Upvotes: 0
Reputation: 18567
More better way to do this is call recursively to array_diff function as below,
$array = array(
46=>86,
47=>86,
48 => [
0=> 88,
1 => 89
]
);
$array1 = [
46 => 64,
47 => 86,
48 => [
0 => 88,
1 => 91
]
];
function array_diff_assoc_recursive($array1, $array2)
{
$difference = array();
foreach ($array1 as $key => $value) {
if (is_array($value)) {
if (!isset($array2[$key]) || !is_array($array2[$key])) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if (!empty($new_diff)) {
$difference[$key] = $new_diff;
}
}
} else if (!array_key_exists($key, $array2) || $array2[$key] !== $value) {
$difference[$key] = $value;
}
}
return $difference;
}
$arr = array_diff_assoc_recursive($array,$array1);
print_r($arr);
I hope this will solve your problem.
Upvotes: 1
Reputation: 92884
all the answers should match exactlly the correct ones, so If I have even a single one wrong I have an error
Use the following approach:
$given_answers = [
46=> "82",
47=> "86",
48=> ["88", "89"],
];
$correct_answers = [
46=> "84",
47=> "86",
48=> ["88", "91"],
];
$all_matched = true;
foreach ($given_answers as $k => $ans) {
if (!is_array($ans)) {
if ($correct_answers[$k] != $ans) { // comparing primitive values
$all_matched = false;
break;
}
} else {
// comparing arrays for equality
if (!empty(array_diff($ans, $correct_answers[$k]))) {
$all_matched = false;
break;
}
}
}
var_dump($all_matched); // false
Upvotes: 1