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

在2d数组中存储值并在PHP中检索,php,arrays,Php,Arrays,我有一个数组,其中存储了一些节名,如a、b、c,在每个节中都有类似“a”的问题,节包含q1、q2、q3。首先,我从myDB中获取这些部分,然后传递这些部分ID以查找查询中的问题 每个问题都有分数。现在我想要的是一个得分为0但有一些适用分数的问题。我希望这些部分和他们的问题一个接一个 这段代码运行良好 见下文 <?php $sectionIDs = $obj->getSectionIDs($formatID); //section ID array starting from

我有一个数组,其中存储了一些节名,如a、b、c,在每个节中都有类似“a”的问题,节包含q1、q2、q3。首先,我从myDB中获取这些部分,然后传递这些部分ID以查找查询中的问题

每个问题都有分数。现在我想要的是一个得分为0但有一些适用分数的问题。我希望这些部分和他们的问题一个接一个

这段代码运行良好

见下文

<?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++;
           }
        }
     }
?>

但在检索中,我遇到了一些问题,比如额外的表行。我怎样才能克服这个问题。我每次都有第二次回声。我想要的是只有当新的部分出现时才能得到这个回音

检索代码:

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

对于($i=0;$i首先,您的检索代码与输出图片不同,因为您的检索代码也会为每个问题显示绿色的剖面栏

其次,我建议以不同的方式设置阵列。您当前的结构:

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?")
)
这意味着,每当每个部分有多个问题时,您都会多次存储该部分名称。您当前的代码假定所有问题都将正确排序。如果数组在任何时候都没有正确排序,则代码将不会显示每个部分的问题

您可以选择一种结构,在这种结构中,您可以使用节作为带有问题的数组的键

$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"
)
使用这样的结构,您可以获取如下输出:

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>';  
    }
}
或者如果您也想要分数:

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

for
循环中删除行
echo'';
,以删除多余的行。是的,我这样做了,但它对显示Sr.获得分数和适用分数的行没有任何影响。可能的解决方案是只为每个部分打印一次!嗯,从你的问题中不清楚你想要哪一行要删除,我的建议删除了空行。是的!可能的解决方案是什么?有什么想法吗?你能告诉我们你不想要哪些行吗?
if($score_achieved == 0 && $score_applicable != 0)
{
    $final_result_arr[$sectionName[$i]][] = array($questionName[$j], 'score', 'score');
}