1 year ago

#368883

test-img

Emre6

Comparing Array data [PHP]

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?

php

arrays

json

array-difference

0 Answers

Your Answer

Accepted video resources