0

In a question form, there is a checkbox for the options of the question. I use the checkboxes as the question has more than one correct answer. When POSTing this form information. (via JSON)

Sample:

{"session":"xxxx","selectedBoxes":["3","1"]}

On the server-side, I want to compare the incoming POST data with the data in the database.

On the server-side the correct answer to the question is stored as:

Sample:

{"0":"1","1":"3"}

The meaning of this data; The first and third options of question are correct.

In a small test environment, I tried:

$array1 = json_decode('{"0":"1","1":"3"}', true);
$array2 = array(3,1);
function array_equal_values(array $a, array $b)
{
return !array_diff($a, $b) && !array_diff($b, $a);
}

if (array_equal_values($array1, $array2) == true) {
    return true;
} else {
    return false;
}

Is this comparison a correct solution?

Emre6
  • 105
  • 3
  • 2
    I think so. Instead of using `array_diff()` twice you could first assert that `count($a) == count($b)` and then just use `array_diff()` once. That makes it slightly easier to understand. – KIKO Software Apr 03 '22 at 08:05
  • @KIKOSoftware was just writing the same comment :) – Nick Apr 03 '22 at 08:05
  • I would say though, if the array contained a large amount of items, count might not be the most efficient solution. You might try hashing both arrays and comparing their hashes. – prog_24 Apr 03 '22 at 08:09
  • @prog_24: Given where the data comes from I don't think efficiency should be a big concern. Also, what if the arrays are in a different order, as in the example? That would give two different hashes, even though the values might be the same. Sorting could solve that, but if you want something inefficient you should use sorting. – KIKO Software Apr 03 '22 at 08:22
  • @KIKOSoftware that is a good point, anyway I believe you are on the right track, perhaps turn your first comment into an answer. – prog_24 Apr 03 '22 at 08:24
  • @KIKOSoftware Thank you for your suggestions. You mentioned good points. I would appreciate it if you could submit it as an answer. – Emre6 Apr 03 '22 at 08:26
  • Ah, see, that's why I didn't try to write an answer. Too often I've been trying to write one when the question was closed while I was doing that. And, of course, Nigel is correct, the answers are already there. – KIKO Software Apr 03 '22 at 08:32
  • @KIKOSoftware This is not true, after all, this question exists and will remain unanswered. Who does it benefit? Is it bad if there is an answer to this question? It would be better to delete the question directly instead of closing the answer. If you have replied, is it possible to share it on PasteBin or a different platform? – Emre6 Apr 03 '22 at 09:00
  • You're asking questions to which I do not know the exact answers. If I'm being positive I would say that duplicates are bad in general, for any site, for instance with respect to SEO. If I'm being negative it could be that users with a high reputation block duplicates to defend their existing answers. But a balanced answer would be that you should have done [your research](https://stackoverflow.com/help/how-to-ask), and if you had, you would have found the answer that Nigel refers to. I also don't think you need a PasteBin to use my comments to update your code. – KIKO Software Apr 03 '22 at 10:24

0 Answers0