0

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

// $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
    // ...
}
4

1 回答 1

1

尝试这个:

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

    }
}

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

于 2012-10-11T00:04:24.900 回答