如何从数组中获取每个question_id和答案?

时间:2018-08-18 15:31:59

标签: php

如果用户正在会议中进行注册,并且正在注册ID为“ 1”(Jake K)的注册类型的1个参与者,以及ID为“ 4”(John W)的注册类型的1个参与者,具有表单数据的请求具有以下3个数组:

"name" => array:2 [▼
  1 => array:1 [▼
    1 => "Jake"
  ]
  4 => array:1 [▼
    1 => "John"
  ]
]
"surname" => array:2 [▼
  1 => array:1 [▼
    1 => "K"
  ]
  4 => array:1 [▼
    1 => "W"
  ]
]
"answer" => array:1 [▼
  1 => array:1 [▼   
    1 => array:2 [▼   // answers answered for the registration_type_id 1
      1 => "answer1"
      2 => "answer2"
    ]
  ]
]

有必要使用此数组的信息插入participantsanswers表中。不能正确插入participants表中,但不能插入answers表中:

 foreach ($request->all()['name'] as $key => $nameArray) {
        foreach ($nameArray as $nameKey => $name) {
            // this is working
            $participant_result = Participant::create([
                'name'                 => $name,
                'surname'              => $request['surname'][$key][$nameKey],
                'registration_id' => $registration->id,
                'registration_type_id' => $key
            ]);


            // save answer to Database if exist
            // this is not working
            $answer = Answer::create([
                'question_id' => $request['answer'][$key], // the issue is here
                'participant_id' => $participant_result->id,
                'answer' => $request['answer'][$key][$nameKey], // and here
            ]);
        }
    }

问题是因为有必要在question_id表的answeranswers列中插入。但是,此值不能从带有“ $request['answer'][$key]”和“ $request['answer'][$key][$nameKey]”的数组中正确返回。

您知道如何从数组中正确获取问题ID和答案吗?

在上面的数组示例中,“ answer1”的问题ID为1,而“ answer2”的问题ID为2。

  "answer" => array:1 [▼
    1 => array:1 [▼
      1 => array:2 [▼
        1 => "answer1"
        2 => "answer2"
      ]
    ]

表格:

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

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

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

    <div class="form-group">
      <label for="participant_question">Question 1</label>
      <input type='text' name='answer[1][1][1]' class='form-control' required>
      <input type="hidden" name="question_required[1][1]" value="1">
    </div>

    <div class="form-group">
      <label for="participant_question">Question 2</label>
      <textarea name='answer[1][1][2]' class='form-control' rows='3' required></textarea>
      <input type="hidden" name="question_required[1][2]" value="1">
    </div>

    <h6> Participant - 1 - plus</h6>

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

    <div class="form-group">
      <label for="surnameplus_1">Surname</label>
      <input type="text"  id="rsurnameplus_1" class="form-control" name=" rsurname[4][1]" value="">
    </div>

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

1 个答案:

答案 0 :(得分:1)

首先:在循环中,您有多个名称,但是答案具有一个键(1)。在第二次运行时,您有$key == 4,但在$request['answer']中没有。也许在第一个循环内您应该有两个循环?一种用于注册参与者,另一种用于注册答案?如果同时注册了答案,则用$participantResult和每个答案寄存器count($participantResult)的答案建立数组:O如果不是,则仅记住第一个注册参与者的ID并保存答案。

请告诉我$request['answer']的工作方式?什么是$request['answer'][1]和什么$request['answer'][1][1]

第二个$request['answer'][$key][$nameKey]是两个元素的数组。如果Answer :: create知道它-没问题;)

编辑

这是正确的解决方案吗?

<?php
$aPID = [];

foreach ($request->all()['name'] as $key => $nameArray) {
    foreach ($nameArray as $nameKey => $name) {
        $aPID[$key] = Participant::create([
            'name'                 => $name,
            'surname'              => $request['surname'][$key][$nameKey],
            'registration_id' => $registration->id,
            'registration_type_id' => $key
        ]);
    }
}

foreach($request['answer'] as $pid => $answersArray) {
    foreach($answersArray as $questionId => $answer) {
        $answer = Answer::create([
            'question_id' => $questionId,
            'participant_id' => $aPID[$pid],
            'answer' => $answer,
        ]);
    }
}