doujia4041 2014-11-10 05:52
浏览 29
已采纳

将值存储在2d数组中并在PHP中检索

I have an array which stores Some section Names Like a,b,c and in each section there are questions like 'a' section contains q1,q2,q3. First i get those sections from myDB and then pass those section ID's to find questions query.

And Each Question has Score. Now what i want is the question which has 0 achieved score but have some applicable Score. i want those sections and their questions one by one.

This piece of code is working fine.

See Below

<?php
     $sectionIDs = $obj->getSectionIDs($formatID); //section ID array starting from 225
     $sectionNames = $obj->getSectionNames($formatID);

     for($i=0; $i<count($sectionIDs); $i++)
     {
        $section_name_print_check ="" ;
        $question_ids = $obj->getQuestionID($formatID,$sectionIDs[$i]);
        $questionName = $obj->getQuestionName($formatID,$sectionIDs[$i]);
        for($j=0; $j<count($question_ids); $j++)
        {
           $score_achieved = $obj->getScore_least($question_ids[$j],$formatID,$last_waveID,$received_ID);
           $score_applicable = $obj->getScore_least_applicabe($question_ids[$j],$formatID,$last_waveID,$received_ID);
           if($score_achieved == 0 && $score_applicable != 0)
           {
               if($section_name_print_check == "" )
               {
                   $final_result_arr[$k]["SectionName"] = $sectionNames[$i];
                   $section_name_print_check = $sectionNames[$j];
               }
               $final_result_arr[$k]["QuestionName"] = $questionName[$j];
               $k++;
           }
        }
     }
?>

But in Retrieval i got some issues like extra table row. How can i get over rid of this problem. I got second echo each time. What i want is to get this echo only when new section comes.

Retrieval Code:

for($i=0; $i<count($final_result_arr); $i++)
{
   echo '<tr style="background-color:#6F6; font-weight:bold;"><td style="width:700px;" colspan="4">'.$final_result_arr[$i]["SectionName"].'</td></tr>';  
   echo '<tr><td style="width:15px;">Sr.</td><td width="399px"></td><td style="width:143px;" align="center">Achieved Score</td><td style="width:143px;" align="center">Applicable Score</td></tr>';
   echo '<tr><td style="width:15px;">'.++$serial.'</td><td style="width:399px;">'.$final_result_arr[$i]["QuestionName"].'</td><td align="center" style="width:143px;">0%</td><td align="center" style="width:143px;">100%</td></tr>';
   echo '<tr><td colspan="5" style="height:25px;"></td></tr>';  
}

The Output is: enter image description here

Here i got Extra Table Row instead of getting one by one sections and question. What could be possible solution for this problem? Should i change my array into simple array or should i change my display section? Any Help! Thanks in Advance

  • 写回答

1条回答 默认 最新

  • duai3681 2014-11-10 09:48
    关注

    First off, your retrieval code is not the same as your output picture as your retrieval code would also show the green section bar for every question aswell.

    Second, I suggest setting up your array in a different way. Your current structure:

    array[0] => (
        array[0] => (['SectionName']  = "Skills Evaluation")
        array[1] => (['QuestionName']  = "Did staff offer any other product?")
    )
    array[1] => (
        array[0] => (['SectionName']  = "Skills Evaluation")
        array[1] => (['QuestionName']  = "Was the conversation closed professionaly?")
    )
    

    This means that whenever you have multiple questions per section you store the section name multiple times. Your current code assumes that the questions will all be sorted correctly. If your array is not at any point sorted correctly, your code will not display the questions per section.

    You could go for a structure where you use the section as a key to an array with questions.

    $final_result_arr['SectionName'][$indexOfQuestion] = "Question";
    
    array['Skills Evaluation'] => (
        array['Skills Evaluation'][0] = "Question"
        array['Skills Evaluation'][1] = "Another question"
    )
    array['Branch environment'] => (
        array['Branch enviroment'][0] = "Question"
    )
    

    With a structure like this you could then fetch the output like this:

    foreach ($final_result_arr as $section => $questions)
    {
        echo '<tr style="background-color:#6F6; font-weight:bold;"><td style="width:700px;" colspan="4">'.$section.'</td></tr>';  
        echo '<tr><td style="width:15px;">Sr.</td><td width="399px"></td><td style="width:143px;" align="center">Achieved Score</td><td style="width:143px;" align="center">Applicable Score</td></tr>';
        foreach ($questions as $question)
        {
            echo '<tr><td style="width:15px;">'.++$serial.'</td><td style="width:399px;">'.$question.'</td><td align="center" style="width:143px;">0%</td><td align="center" style="width:143px;">100%</td></tr>';
            echo '<tr><td colspan="5" style="height:25px;"></td></tr>';  
        }
    }
    

    You'll obviously have to adjust your code to fill up the array though, if you then want to also store the achieved and applicable score you could instead make $question an array and have it store those values.

    Filling up the array:

    if($score_achieved == 0 && $score_applicable != 0)
    {
        $final_result_arr[$sectionName[$i]][] = $questionName[$j];
    }
    

    Or if you want the scores aswell:

    if($score_achieved == 0 && $score_applicable != 0)
    {
        $final_result_arr[$sectionName[$i]][] = array($questionName[$j], 'score', 'score');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错