duanche2007 2013-01-14 21:24
浏览 28
已采纳

如何阻止钥匙中的空隙?

http://phpfiddle.org/main/code/get-rps

Now it is not shown in the above fiddle that it is able to insert all the data. But in application it is not displaying all incorrect answers in the table.

The error for this is caused by the fact that array is iterating over $ques_ans with for() has a gap in keys.

var_dump($ques_ans) gives us:

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

There is no element with the key [1]. It's because the function array_intersect, which I use on line 49, preserves keys.

To quickly fix the code just to make it work without errors I added array_values() on line 51:

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

But it still does have missing keys and hence not all incorrect answers are displayed. What else am I doing wrong here?

The big problem is that if a question has multiple correct answers, then in the array for incorrect answers, it displays multiple sets of answers for those questions and just removes one correct answer at a time for each set of answers. You will see this in the array for incorrect answers.

Below is a screenshot showing the a missing incorrect answer and a missing key but how can I fix this?

enter image description here

  • 写回答

1条回答 默认 最新

  • douyong1886 2013-01-15 13:46
    关注

    Use array_values.

    foreach($ques_ans as $questionNo => $inc_ans)
    {
        $inc_ans = array_values($inc_ans);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?