如何分开答案

时间:2012-10-10 23:51:24

标签: php

下面我有一段代码,结合了每个问题的每个答案:

// $questionNumber is simply n in value[n][]
// $answers is an array of value attributes from <input>s for this question

foreach ($_POST['value'] as $questionNumber => $answers) {
    // combine all the answers into a single string (e.g. ACE)
    $selected_answer = implode('', $answers);

    // continue onto inserting rows into the Answer and Questions tables
    // ...
}

所以例如:

Question 1: Answer: ACE
Question 2: Answer: BD
Question 3: Answer: C

但我实际上想要分开答案,以便它显示如下:

Question 1: Answer: A
Question 1: Answer: C
Question 1: Answer: E
Question 2: Answer: B
Question 2: Answer: D
Question 3: Answer: C

那么我的问题是我需要在我的php中进行哪些更改才能将它们分开?

假设如下:

// $questionNumber is simply n in value[n][]
// $answers is an array of value attributes from <input>s for this question

foreach ($_POST['value'] as $questionNumber => $answers) {
    // combine all the answers into a single string (e.g. ACE)
    $selected_answer = $answers;

    // continue onto inserting rows into the Answer and Questions tables
    // ...
}

1 个答案:

答案 0 :(得分:1)

试试这个:

foreach ($_POST['value'] as $questionNumber => $answers) 
{
    foreach($answers as $a)
    {  
             echo "Question: $questionNumber Answer: $a";

    }
}

循环内部的循环似乎会输出您想要的内容。如果您需要进一步的帮助,请在$ _POST上运行print_r()并给我答案。