使用php

时间:2019-03-21 04:14:18

标签: php arrays

你好,请帮我,我在表data_result中有这样的数据

  | uniqueid | question | answers | remark |
  | 10001 | Q0003,Q0001,Q0004| Yes,No,Yes | ,none,yes |
  | 10002 | Q0002,Q0009,Q0008 | No,Yes,Yes | yes,,No |
  | 10003 | Q0007,Q0003,Q0006 | Yes,Yes,Yes | ,,, |

我有桌子master_question

  | que_code | question |
  | Q0001  | Where do you live ? |
  | Q0002  | Your birthday ? |
  | Q0003  | ...... |
  | ....   | ...... |

我想显示这样的结果

|uniqueid|Q0001|remark|Q0002|remark|Q0003|remark| ...
|10001 | No | none | - | - | Yes | - | ...
|10002 | - | - | No | Yes | - | - | ...

我尝试这样做,但结果不正确

foreach ($get_data_all->result() as $data) {
    $resultdata[$data->uniqueid]['uniqueid'] = $data->uniqueid;
    $que = $data->questionList;
    $ans = $data->answerList;
    $rem = $data->remarksList;

    $question = explode(',',$que);
    $answer = explode(',',$ans);
    $remark = explode(',',$rem);

    $new = array();
    for($i=0;$i<count($question);$i++){
        if(isset($question[$i])){
            $question[$i] = $question[$i];
        }
        else {
            $question[$i] = null;
        }
        if(isset($answer[$i])){
            $answer[$i] = $answer[$i];
        }
        else {
            $answer[$i] = null;
        }
        if(isset($remark[$i])){
            $remark[$i] = $remark[$i];
        }
        else {
            $remark[$i] = null;
        }
        $new[$i] = array("question"=>$question[$i],"answer"=>$answer[$i],"remark"=>$remark[$i]);
    }

    $master_question = $this->m_report_result->master_question();

    foreach ($master_question->result() as $mque) {
        foreach ($new as $nilai) {
            $nilai = (object)$nilai;
            //echo "$nilai->question\n";
            //echo "$nilai->answer\n";
            //echo "$nilai->remark\n";
            if($mque->question_code == $nilai->question){
                $resultdata[$data->uniqueid]['answer'] = $nilai->answer;
                $resultdata[$data->uniqueid]['remark'] = $nilai->remark;  
            }else{
                $resultdata[$data->uniqueid]['answer'] = '';
                $resultdata[$data->uniqueid]['remark'] = '';
            }
        }
    }
}

对此一无所知,我尝试循环访问主问题,并用问题代码匹配答案和备注值。

1 个答案:

答案 0 :(得分:0)

使$new为关联数组,其键为问题ID。这样就不需要嵌套循环,可以使用isset()来测试该问题是否有结果。

$question_list = $this->m_report_result->master_question()->result();
echo "<table><tr><th>uniqueid</td>";
foreach ($question_list as $mque) {
    echo "<th>$mque->question_code</th><th>remark</th>";
}
echo "</tr>";

foreach ($get_data_all->result() as $data) {
    $que = $data->questionList;
    $ans = $data->answerList;
    $rem = $data->remarksList;

    $questions = explode(',',$que);
    $answers = explode(',',$ans);
    $remarks = explode(',',$rem);

    $new = array();
    foreach ($questions as $i => $question){
        $answer = $answers[$i] ?? null;
        $remark = $remarks[$i] ?? null;
        $new[$question] = array("answer"=>$answer[$i],"remark"=>$remark[$i]);
    }
    echo "<tr><td>$data->uniqueid]</td>";
    foreach ($question_list as $mque) {
        $que_code = $mque->question_code;
        if (isset($new[$que_code])) {
            echo "<td>{$new[$que_code]['answer']}</td><td>{$new[$que_code]['remark']}</td>";
        } else {
            echo "<td>-</td><td>-</td>";
        }
    }
    echo "</tr>";
}
echo "</table>";