Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何正确地从数组中获取信息,以便能够插入到数据库中?_Php - Fatal编程技术网

Php 如何正确地从数组中获取信息,以便能够插入到数据库中?

Php 如何正确地从数组中获取信息,以便能够插入到数据库中?,php,Php,用户填写表格注册两名与会者并单击“注册”后,会出现一个包含请求数据的数组 请求数据具有数组name,该数组存储注册类型id“1”(Jake)中注册的参与者的姓名和注册类型id“4”(John)中注册的参与者的姓名 与姓氏数组相同,此姓氏数组存储每个注册类型的每个姓氏 "name" => array:2 [▼ 1 => array:1 [▼ 1 => "Jake" ] 4 => array:1 [▼

用户填写表格注册两名与会者并单击“注册”后,会出现一个包含请求数据的数组

请求数据具有数组
name
,该数组存储注册类型id“1”(Jake)中注册的参与者的姓名和注册类型id“4”(John)中注册的参与者的姓名

姓氏
数组相同,此姓氏数组存储每个注册类型的每个姓氏

 "name" => array:2 [▼
        1 => array:1 [▼
          1 => "Jake"
        ]
        4 => array:1 [▼
          1 => "John"
        ]
      ]
      "surname" => array:2 [▼
        1 => array:1 [▼
          1 => "W"
        ]
        4 => array:1 [▼
          1 => "K"
        ]
      ]
      "answer" => array:1 [▼
        1 => array:1 [▼
          1 => array:6 [▼
            1 => "answer1"
            2 => "answer2"
          ]
        ]
      ]
对于存储每个注册类型的每个答案的
answer
数组也是如此

 "name" => array:2 [▼
        1 => array:1 [▼
          1 => "Jake"
        ]
        4 => array:1 [▼
          1 => "John"
        ]
      ]
      "surname" => array:2 [▼
        1 => array:1 [▼
          1 => "W"
        ]
        4 => array:1 [▼
          1 => "K"
        ]
      ]
      "answer" => array:1 [▼
        1 => array:1 [▼
          1 => array:6 [▼
            1 => "answer1"
            2 => "answer2"
          ]
        ]
      ]
疑问:我的疑问是如何从上面的数组结构中获取信息,以便将每个参与者存储在参与者表中,以及如何将每个答案存储在答案表中

使用上面的数组内容,参与者表应保持如下状态:

 id      registration_type_id                name             surname        registration_id
 1            1                            Jake                 W                 1 
 2            4                           John                 K                 1
 id       participant_id          question_id        answer
     1              1                  1              answer1
     1              1                  2              answer2
答案表应保持如下状态:

 id      registration_type_id                name             surname        registration_id
 1            1                            Jake                 W                 1 
 2            4                           John                 K                 1
 id       participant_id          question_id        answer
     1              1                  1              answer1
     1              1                  2              answer2
你知道如何正确地做到这一点吗

我有下面的代码,首先在registrations表中插入一个条目来存储注册:

# user object
$user = Auth::user();
# add registration to Database
$registration = Registration::create([
    'conference_id' => $id,
    'user_that_did_registration' => $user->id]
);
但随后需要将每个参与者存储在参与者表中,并将每个答案存储在答案表中。您知道如何正确地从上面的数组中获取必要的信息,并使用下面的代码存储在参与者表和答案表中吗

要在参与者表中插入,必须使用:

$participant_result = Participant::create([
      'name' => how to get the name of each participant from the array?,
      'surname' => how to get the surname of each participant from the array?,
      'registration_id' => $registration->id,
      'registration_type_id' => how to get the registration_type_id from the array?
  ]);
 $answer = Answer::create([
        'question_id' => how to get each question id from the array?,
        'participant_id' => $participant_result->id,
        'answer' => how to  get each answer from the array?
    ]);
要在答案表中插入,必须使用:

$participant_result = Participant::create([
      'name' => how to get the name of each participant from the array?,
      'surname' => how to get the surname of each participant from the array?,
      'registration_id' => $registration->id,
      'registration_type_id' => how to get the registration_type_id from the array?
  ]);
 $answer = Answer::create([
        'question_id' => how to get each question id from the array?,
        'participant_id' => $participant_result->id,
        'answer' => how to  get each answer from the array?
    ]);

我不完全理解您的项目,但我觉得您的输入阵列中有额外/不必要的级别。我也不知道为什么你的钥匙从1开始。您仅仅需要一种方法来遍历数组吗?这离你有多近?谢谢,但例如在参与者表中,需要存储姓名、姓氏和注册类型id吗?需要3个foreach,一个用于获取姓名,另一个用于获取每个参与者的姓氏和注册类型id吗?数组有索引1和4,因为是用户进行注册的注册类型,用户正在会议中注册,对于注册,他为id为“1”的注册类型选择了数量“1”,为id为4的注册类型选择了数量“1”。例如,数组名存储注册类型id为“1”的参与者的姓名(Jake)以及在注册类型id“4”(John)中注册的参与者的名称:“name”=>数组:2[▼ 1=>数组:1[▼ 1=>“Jake”]4=>数组:1[▼ 1=>“John”]>。相同的姓氏存储每个注册类型的每个姓氏,相同的答案数组存储每个注册类型的每个答案。这样,似乎可以存储每个参与者的姓名和姓氏。但是如何获取每个参与者的注册类型id?”foreach($request->all()['name']as$key=>$name){$participant\u result=participant::create(['name'=>$name[1],'name'=>$request['name'][$key][1],'registration\u id'=>$registration->id,'registration\u type\u id'=>如何获取此信息?);}”。