There are two JSON data. I want to ensure that the answers to the question are written in complex form. But I couldn’t do it.
I can get a single JSON when I can, but I get the error when there is more than one JSON
Questions:
The total number of people in the world?
// plaint text
Options:
{'opt1':'4 Billion','opt2':'5 Billion','opt3':'6 Billion','opt4':'7 Billion'}
// JSON
Answer:
{"0":"2","1":"3"}
// coorect answers: 2. & 3. options (multiple)
$options = json_decode($quiz->options);
$answers = json_decode($quiz->answerOfQuestion, true);
foreach ($options as $key => $firstvalue) {
if (in_array(substr($key, -1), $answers)) {
// correct options
echo "<input type='checkbox' value='".substr($key, -1)."'>";
} else {
// wrong options
echo "<input type='checkbox' value='".substr($key, -1)."'>";
What have I done?
$options = shuffle(json_decode($quiz->options));
$answers = shuffle(json_decode($quiz->answerOfQuestion, true));
Error:
Unknown error type: [8] Only variables should be passed by reference
Unknown error type: [2] shuffle() expects parameter 1 to be array, object given
Unknown error type: [8] Only variables should be passed by reference
Unknown error type: [2] Invalid argument supplied for foreach()
How can I make the shuffle
writing ?
I don’t understand the code, but 2 annotations:
if your decode something like "{...}
", you get an Object, not an Array.
shuffle()
is made for randomzing arrays, not objects, so the error.
An array encoded looks like this: “[...]
”
If you add an true in your options, it may work perhaps: json_decode($quiz->options, true)
you are shuffling options AND answers.
So they have different orders! I’m not sure whether this is intended.