dongying3830 2011-05-07 15:05
浏览 67

构建表单输入的多维数组

PHP supports:

<input type=text name=array[]>

...and all values are just posted in that array. I want to do the same for a multidimensional? array, however I am getting slightly lost in trying to extract data.

I have a set of questions, most have one answer (radio buttons) however some are multi-response (checkboxes). I want to be able to store all answers in one multidimensional array so it may appear like this:

question 1 | answer |
question 2 | answer |
question 3 | answer |
question 3 | answer |
question 3 | answer |
question 4 | answer |

So, I am doing this:

<input name="response['q1'][]" type=text value=''>
<input name="response['q2'][]" type=text value=''>
<input name="response['q3'][]" type=text value=''>
<input name="response['q3'][]" type=text value=''>

Now how can I go about extracting this data for storing into my mysql db. Ive been playing around with a foreach, however when it is the case there is more than one answer for a question (q3 lets say) I can't get that data in my loop.

  • 写回答

1条回答 默认 最新

  • dongqiao3833 2011-05-07 15:14
    关注

    Try leaving the quotes away (name="response[q3][]") and then loop using foreach($_POST['response']['q3'] as $key => $value) { echo $value." "; }

    foreach($_POST['response'] as $question => $answer) {
      if(count($answer) > 1) {
        foreach($answer as $key => $value) {
          //loop your multiple answers here
        }
      } elseif(count($answer) == 1 {
         // 1 answer found for $_POST['response'][ $question ];
      } else {
        // no answer found.. (perhaps $_POST['response'][ $key ] is a value and no array.
      }
    }
    

    I guess this is what you're looking for.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog