I’m programming a PHP quiz application and am having some trouble with the scoring mechanism. Specifically, I have 2 arrays that I’m comparing to determine if an answer is correct or not.
I want to verify that all values of one array are found in another array. For example, if the correct answer
Array
(
[0] => Proprietary user community
[1] => Surveys
[2] => Voice
[3] => Online chat
[4] => Web
[5] => Email
[6] => Social media
)
And the user provided answer is:
Array
(
[0] => Surveys
[4] => Online chat
[6] => Email
)
The system return incorrect, because all correct values have not been provided. Similarly, if the user provided answer looks like this:
Array
(
[0] => Proprietary user community
[1] => Surveys
[2] => Voice
[3] => Online chat
[4] => Web
[5] => Email
[6] => Social media
[7] => Phone
[8] => Live chat
)
The the answer would be correct, if though additional answers have been provided.
Any ideas? I’ve been thinking of using array_intersect() but there has to be a more elegant solution.
Any help is much appreciated!
Just intersect the arrays and compare sizes.