如果用户正在会议中进行注册,并且为常规注册类型选择数量“ 1”,为注册类型plus选择数量“ 1”,然后单击“下一步”进入注册表单。
在表格中,用户需要为每个参与者介绍姓名和姓氏,这两个字段对于每个正在注册的参与者始终是必填字段。然后,一般注册类型具有3个与之相关的自定义问题,因此对于一般注册类型,用户还需要回答这3个自定义问题。用户在注册表中提出的问题和答案如下:
Question Answer
input text custom question text answer
long text custom question: long answer
checkbox custom question: check1answer
使用下面的注册表和上面的答案,$request->all()
中的参与者数组如下所示。名称,姓氏,rtypes已正确存储,但答案和question_id未正确存储在数组中:
"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" => "check1answer"
]
5 => array:1 [▼
"question_id" => "3"
]
"rtypes" => "1"
]
2 => array:3 [▼
"name" => "Jake"
"surname" => "K"
"rtypes" => "4"
]
]
存储的数据像使用此foreach插入参与者一样,并且数据库中的答案在$ participant ['answer']中显示“未定义索引:答案”:
foreach ($participants_list as $participant) {
$name = $participant['name'];
$surname = $participant['surname'];
$participant_result = Participant::create([
'name' => $name,
'surname' => $surname,
'registration_type_id' => $participant['rtypes']
]);
Answer::create([
'participant_id' => $participant_result->id,
'answer' => $participant['answer'],
'question_id' => $participant['question_id']
]);
}
您知道构造参与者数组以解决问题吗?这样可以正确地将每个参与者的答案和question_id存储在答案表中吗?
The $participant[0]['answer'] shows "text answer".
The $participant[1]['answer'] shows "undefined index answer".
The $participant[2]['answer'] shows "long answer".
The $participant[3]['answer'] shows "undefined index answer".
我是初学者,但也许像这样的结构允许使用foreach存储在答案表中。数组的每个索引都存储与每个参与者相关的所有信息(姓名,姓氏,正在注册参与者的注册类型,如果还存在与注册类型相关的自定义问题,则还需要存储答案:
"participant" => array:2 [▼
1 => array:15 [▼
"name" => "John"
"surname" => "W"
"answer" => [
0 => "text answer"
1 => "long answer"
2 => "check1"
]
"question_id" => [
0 => "1"
1 => "2"
2 => "3"
]
"rtypes" => "1"
]
2 => array:3 [▼
"name" => "Jake"
"surname" => "K"
"rtypes" => "4"
]
]
索引2中的参与者正在以ID为4的注册类型“ plus”进行注册,并且此注册类型没有任何关联的自定义问题。因此只需要收集名称,姓氏和注册类型ID。因此,对于参与者2来说还可以,因为没有自定义问题。
注册表格:
<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][rtypes]" 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][rtypes]" value="4"/>
<input type="submit" class="btn btn-primary" value="Register"/>
</form>
答案 0 :(得分:1)
您需要以某种方式处理原始代码,以便仅通过该代码获得所需的格式。
但是,如果您无法控制该代码,请尝试使用以下代码来操纵当前的数组结构,使其以所需的格式显示:-
$final = array();
foreach($array['participant'] as $key=>$val){
$final['participant'][$key]['name'] = $val['name'];
$final['participant'][$key]['surname'] = $val['surname'];
$answer_array = array_column($val,'answer');
$question_id_array = array_column($val,'question_id');
if(is_array($answer_array) && count($answer_array) >0){
$final['participant'][$key]['answer'] = $answer_array;
}
if(is_array($question_id_array) && count($question_id_array) >0){
$final['participant'][$key]['question_id'] = $question_id_array;
}
$final['participant'][$key]['rtypes'] = $val['rtypes'];
}
print_r($final);