How I can shuffle multiple JSON data in foreach? (PHP)

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)

Code

   $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:

  1. 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)
  2. you are shuffling options AND answers.
    So they have different orders! I’m not sure whether this is intended.

Thank you for your interest.

This error is a result of trying to pass value by reference. Since objects in PHP use only references… I suggest assigning the $quiz->options to a variable, then pass in that variable.