简体   繁体   English

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

[英]Store values in 2d array and retrieve in 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. 我有一个存储一些部分名称(如a,b,c)的数组,并且在每个部分中都有类似“ a”部分的问题,其中包含q1,q2,q3。 First i get those sections from myDB and then pass those section ID's to find questions query. 首先,我从myDB获取这些部分,然后传递这些部分ID来查找问题查询。

And Each Question has Score. 每个问题都有分数。 Now what i want is the question which has 0 achieved score but have some applicable Score. 现在我想要的是获得0分但有一些适用分数的问题。 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: 输出为: 在此处输入图片说明

Here i got Extra Table Row instead of getting one by one sections and question. 在这里,我得到了Extra Table Row,而不是一个接一个地回答问题。 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 提前致谢

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. 显然,您显然必须调整代码来填充数组,如果您还想存储已实现的分数和适用分数,则可以将$ question设置为数组并存储这些值。

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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM