2

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
ltdev
  • 4,037
  • 20
  • 69
  • 129
  • 1
    Possible duplicate of [Compare multidimensional arrays in PHP](http://stackoverflow.com/questions/7389176/compare-multidimensional-arrays-in-php) – Rohan Khude Jan 25 '17 at 09:46
  • and how should look the final array/result? – RomanPerekhrest Jan 25 '17 at 09:52
  • The logic I'm trying to do is all the answers should match exactlly the correct ones, so If I have even a single one wrong I have an error (i.e failed to pass the questionnaire), so it dosen't matter if I have an array of wrong answers or just a `false` – ltdev Jan 25 '17 at 09:57

2 Answers2

1

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
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
1

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.

Rahul
  • 18,271
  • 7
  • 41
  • 60