PHP check if array values match another array's values in the same position

Solution:

You should use array_intersect_assoc();

So your code would become...

$user_answers = array(1,3,1);
$answer_key = array(3,1,1);
$result = array_intersect_assoc($user_answers, $answer_key);
echo count($result);

Which will give a result of 1.

See: https://www.php.net/manual/en/function.array-intersect-assoc.php

Answer

Solution:

array_intersect_assoc() function compare array and association key also. try following code

   $user_answers = array(1,3,1,5,8,8,7);
    $answer_key = array(3,1,1,5,7,9,7);
    $result = array_intersect_assoc($user_answers, $answer_key);
    echo count($result);

Output

3

Source