【发布时间】:2019-05-30 23:03:49
【问题描述】:
这和其他问题的逻辑不一样。
有两个 JSON 数据。我想确保问题的答案以复杂的形式编写。但我做不到。我可以得到一个 JSON,但是当有多个 JSON 时我得到错误
问题:世界上有多少人?
选项:
{'opt1':'4 Billion','opt2':'5 Billion','opt3':'6 Billion','opt4':'7 Billion'}答案:
{"0":"2","1":"3"}// 正确答案:2. & 3. 选项 (多个)
代码
$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)."'>";
}
}
我做了什么?
$options = shuffle(json_decode($quiz->options));
$answers = shuffle(json_decode($quiz->answerOfQuestion, true));
错误:
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()
我怎样才能使复杂的写作shuffle?
【问题讨论】:
-
如果您想使用数组,您需要将
true作为第二个参数传递给您的 json_decode 函数。否则你会得到一个对象。您正在为 answerOfQuestion 而不是选项这样做。在结果上尝试var_dump()以帮助您解决问题