gpt4 book ai didi

php - 如何正确设置循环以显示所有信息

转载 作者:可可西里 更新时间:2023-10-31 23:49:11 24 4
gpt4 key购买 nike

尝试将每个问题的所有错误答案显示到一个表格中,但出于某种奇怪的原因,我在其中一行中收到了一个空答案。现在这只发生在问题 2 中,问题 2 是唯一有多个正确答案的问题,所以我相信我遇到的问题是我没有正确设置 $row_count我的问题是 如何正确设置才能显示缺少的答案 D?查看问题的php fiddle在这里http://phpfiddle.org/main/code/get-rps

下面的代码为包含多个正确答案的问题设置错误答案的行数如下:

 <?php
//remaining incorrect answers in separate row (if any) follows here
if($row_count < $q_row_span - 1)
{
for($i=($row_count + 1); $i<$q_row_span; $i++) { ?>
<tr>
<td class="answertd"><?php echo$inc_ans[$i];?>
<input type="hidden" id="hiddenincorrect" name="incorrect[]" value="<?php echo$inc_ans[$i];?>">
</td>

<td class="answermarkstd">
<input class="individualMarks q<?php echo$questionNo?>_mark" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="<?php echo$questionNo?>" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
</tr>
<?php
}

最佳答案

该错误是由于您使用 for() 遍历 $ques_ans 的数组在键中存在间隙而导致的。

var_dump($ques_ans) 给我们:

array(3) {
... skipped for brevity
[2]=>
array(2) {
[0]=>
string(1) "B"
[2]=>
string(1) "D"
}
... skipped for brevity
}

没有键为[1] 的元素。这是因为函数 array_intersect ,您在第 49 行使用的,保留 key 。

为了快速修复您的代码,使其正常工作,我在第 51 行添加了 array_values():

$ques_ans[$questionNo] = array_values($q_incorrect_ans);    //store the array of incorrect ans against the ques no as key

尽管它可能对您的应用逻辑没有意义。我只是没有潜入其中。所以检查一下。

关于php - 如何正确设置循环以显示所有信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14308941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com