Php 如何将每个答案存储在答案表中?(如何在阵列中正确存储)

Php 如何将每个答案存储在答案表中?(如何在阵列中正确存储),php,arrays,Php,Arrays,如果用户选择数量“1”表示注册类型“常规”,选择数量“1”表示注册类型“加号”,然后单击“下一步”并转到注册表 在表单中,用户需要介绍每个参与者的姓名。然后,注册类型general有3个与之关联的自定义问题,因此对于注册类型general,用户还需要回答这3个自定义问题。用户在注册表中回答的问题和答案如下: Question Answer input text custom question text

如果用户选择数量“1”表示注册类型“常规”,选择数量“1”表示注册类型“加号”,然后单击“下一步”并转到注册表

在表单中,用户需要介绍每个参与者的姓名。然后,注册类型general有3个与之关联的自定义问题,因此对于注册类型general,用户还需要回答这3个自定义问题。用户在注册表中回答的问题和答案如下:

Question                                  Answer
input text custom question                text answer
long text custom question:                long answer
checkbox custom question:                 check1
按照注册表格的原样和上面的答案,
$request
中的参与者数组如下所示。名称、姓氏、regtypes存储正确,但数组中的答案存储不正确:

 "participant" => array:2 [▼
        1 => array:15 [▼
          "name" => "John"
          "surname" => "W"
          0 => array:1 [▼
            "answer" => "text answer"
          ]
          1 => array:1 [▼
            "question_id" => "1"
          ]
          2 => array:1 [▼
            "answer" => "long answer"
          ]
          3 => array:1 [▼
            "question_id" => "2"
          ]
          4 => array:1 [▼
            "answer" => "check1"
          ]
          5 => array:1 [▼
            "question_id" => "3"
          ]
          "regtypes" => "1"
        ]
        2 => array:3 [▼
          "name" => "Jake"
          "surname" => "K"
          "regtypes" => "4"
        ]
    ]
使用此foreach将数据存储为插入参与者,数据库中的答案在
$participant['answer']
中显示“Undefined index:answer”:

foreach ($participants_list as $participant) {

    $name = $participant['name'];
    $surname = $participant['surname'];

    $participant_result = Participant::create([
         'name' => $name,
         'surname' => $surname,
         'registration_type_id' => $participant['regtypes']
    ]);

    Answer::create([
        'participant_id' => $participant_result->id,
        'answer' => $participant['answer'],
        'question_id' => $participant['question_id']
    ]);
}
你知道会有什么问题吗?可能是因为答案和问题id是如何存储在数组中的,但我不知道如何正确更正

登记表格:

<form method="post" action="https://proj.test/conf/1/conf-test/registration/store">
  <h6>Participant - 1 - general</h6>

  <div class="form-group">
    <label for="namegeneral_1">Name</label>
    <input type="text" required id="namegeneral_1" name="participant[1][name]" class="form-control" value="">
  </div>

  <div class="form-group">
    <label for="surnamegeneral_1">Surname</label>
    <input type="text" required id="surnamegeneral_1" class="form-control" name="participant[1][surname]" value="">
  </div>

  <div class="form-group">
    <label for="participant_question">input text custom question</label>
    <input type='text' name='participant[1][][answer]' class='form-control' required>
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden"  value="1" name="participant[1][][question_id]"/>
  </div>

  <div class="form-group">
    <label for="participant_question">long text custom question</label>
    <textarea name='participant[1][][answer]' class='form-control' rows='3' required></textarea>
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="2" name="participant[1][][question_id]"/>
  </div>

  <div class="form-group">
    <label for="participant_question">checkbox custom question</label>
    <div class='checkbox-group required'> 
      <div class='form-check'>
        <input type='checkbox' name='participant[1][][answer]' value='select1' class='form-check-input' >
        <label class='form-check-label' for='exampleCheck1'>check1</label>
      </div> 
      <div class='form-check'>
        <input type='checkbox' name='participant[1][][answer]' value='select2' class='form-check-input' >
        <label class='form-check-label' for='exampleCheck1'>check2</label>
      </div>
    </div>
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="3" name="participant[1][][question_id]"/>
  </div>

  <input type="hidden" name="participant[1][regtypes]" value="1"/>

  <h6>Participant - 2 - plus</h6>

  <div class="form-group">
    <label for="nameplus_2">Name</label>
    <input type="text" required id="nameplus_2" name="participant[2][name]" class="form-control" value="">
  </div>

  <div class="form-group font-size-sm">
    <label for="surnameplus_2">Surname</label>
    <input type="text" required id="surnameplus_2" class="form-control" name="participant[2][surname]" value="">
  </div>

  <input type="hidden" name="participant[2][regtypes]" value="4"/>

  <input type="submit" class="btn btn-primary" value="Register"/>
</form>

参与者-1-概述
名称
姓
输入文本自定义问题
长文本自定义问题
复选框自定义问题
支票1
支票2
参加者-2人以上
名称
姓

在循环中,您可以执行以下操作:

foreach ($participants_list as $participant) {
因此,
$participant
是:

1 => array:15 [▼
          "name" => "John"
          "surname" => "W"
但是,
answer
键位于上面的子数组中,因此从上面的数组中它是下一级:

      0 => array:1 [▼
        "answer" => "text answer"
因此,
answer
键不在
$participant
循环值数组中,完全按照您的数组:

 "participant" => array:2 [▼
        1 => array:15 [▼
          "name" => "John"
          "surname" => "W"
          0 => array:1 [▼
            "answer" => "text answer"

answer
将位于
$participant[0]['answer']

谢谢,但您知道如何正确解决该问题吗?是否需要更改参与者数组结构更改输入的name属性?或者更改foreach?将foreach更改为“foreach($k=>$participant){”,并使用$participant[$k]['answer']显示相同的未定义问题。可能需要更改数组结构,因为,$participant[0]['answer']显示“文本答案”;$participant[1]['answer']显示“未定义的索引”\U答案”,$participant[2]['answer']显示“长答案”,$participant[3]['answer']显示“未定义的索引答案”。数组的问题是您有多个子数组,其键为
question\u id
answer
,而键只是数字键(0、1、2等),因此无法识别哪个“答案”或“question\u id”你需要。因此,是的,你需要重新考虑你的数组的结构。我不知道你正试图从这个数组中做什么,但我猜你可能想用一些问题来整理一个用户,这可能意味着将用户id作为主键,然后是一个子数组及其详细信息、名称等,然后是另一个子数组中的键为“问题”,多个子数组中的键为问题id,其中包含问题和答案。如果有意义的话。很难在注释中理解和解释