Php 如何解决此未定义的偏移量:0错误?

Php 如何解决此未定义的偏移量:0错误?,php,laravel,Php,Laravel,在“大会详细信息”页面中,用户为每种票证类型选择所需数量,然后单击“下一步”并转到注册页面。在注册页面中有注册表格 Congressions表有一列“所有参与者”: 如果所有_参与者均为“0”,则表示只需要收集有关正在进行注册的用户的信息,即经过身份验证的用户。身份验证用户的姓名、姓氏和电子邮件用于注册。在注册表中,只需显示一次与所选票证类型关联的自定义问题(对于认证用户答案),并且应使用认证用户的id存储 因此,如果所有_参与者都是“0”,并且用户在上一页中选择了票证类型,则 在注册表中关联

在“大会详细信息”页面中,用户为每种票证类型选择所需数量,然后单击“下一步”并转到注册页面。在注册页面中有注册表格

Congressions表有一列“所有参与者”:

  • 如果所有_参与者均为“0”,则表示只需要收集有关正在进行注册的用户的信息,即经过身份验证的用户。身份验证用户的姓名、姓氏和电子邮件用于注册。在注册表中,只需显示一次与所选票证类型关联的自定义问题(对于认证用户答案),并且应使用认证用户的id存储
  • 因此,如果所有_参与者都是“0”,并且用户在上一页中选择了票证类型,则 在注册表中关联一个或多个自定义问题 显示自定义问题。如果没有与用户选择的任何票证类型相关的自定义问题,则用户无需插入任何信息,因为其身份验证信息(姓名、姓氏和电子邮件)用于注册
问题是,当用户填写字段并单击“转到步骤2”时,会出现“
未定义的偏移量:0

你知道如何妥善解决这个问题吗

用图表解释的错误:(在这种情况下,有一个自定义问题“What's your phone?”与用户选择的至少一种票证类型关联)

//登记表

<form method="post" id="step1form" action="">
        {{csrf_field()}}
        @if (!empty($allParticipants))
            @if($allParticipants == 1)
                <p>Please fill in all fields. Your tickets will be sent to
                    p{{ (\Auth::check()) ? Auth::user()->email : old('email')}}.</p>

                @foreach($selectedTypes as $selectedType)
                    @foreach(range(1,$selectedType['quantity']) as $test)

                        <h6>Participant - 1 - {{$test}}</h6>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="" value="">
                            <label class="form-check-label d-flex align-items-center" for="exampleRadios1">
                                <span class="mr-auto">Fill the following fields with the authenticated user information.</span>
                            </label>
                        </div>
                        <div class="form-group font-size-sm">
                            <label for="participant_name" class="text-gray">Name</label>
                            <input type="text" name="participant_name[]" required class="form-control" value="">
                        </div>
                        <div class="form-group font-size-sm">
                            <label for="participant_surname" class="text-gray">Surname</label>
                            <input type="text" required class="form-control" name="participant_surname[]" value="">
                        </div>
                        <input type="hidden" name="ttypes[]" value="{{ $selectedType['id'] }}"/>
                        @foreach($selectedType['questions'] as $customQuestion)
                            <div class="form-group">
                                <label for="participant_question">{{$customQuestion->question}}</label>
                                <input type="text"
                                       @if($customQuestion->pivot->required == "1") required @endif
                                       class="form-control" name="participant_question[]">
                                <input type="hidden" name="participant_question_required[]"
                                       value="{{ $customQuestion->pivot->required }}">
                                <input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
                            </div>
                        @endforeach
                    @endforeach
                @endforeach
                @else
                    <p>Its not necessary aditional info. Your tickets will be sent to {{ (\Auth::check()) ? Auth::user()->email : old('email')}}.</p>

                  @if($selectedRtype['questions'] )
                      <p>You only need to answer the cutom questions below.</p>
                @foreach($selectedRtype['questions'] as $customQuestion)
                    <div class="form-group">
                        <label for="participant_question">{{$customQuestion->question}}</label>
                        <input type="text"
                               @if($customQuestion->pivot->required == "1") required @endif
                               class="form-control" name="participant_question[]">
                        <input type="hidden" name="participant_question_required[]"
                               value="{{ $customQuestion->pivot->required }}">
                        <input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
                    </div>
                @endforeach
        @endif

                @endif
            @endif

        <input type="submit" href="#step2"
               id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
    </form>

只需搜索
0
:访问数组的键时,必须确保该键存在

对于
$I=0的循环,我看到了4用于访问数组但从未选中的声明。另外,您可能只想使用foreach循环,但这取决于您自己

检查密钥是否存在

if (isset($request->participant_question_id[$i])) {
    $answer = Answer::create([
        'question_id' => $request->participant_question_id[$i],
        // the error undefined offset is here
        'participant_id' => $participants[$i]->id,
        'answer' => $request->participant_question[$i],
    ]);
}
foreach方法

foreach ($request->participant_question as $key => $question)
    $answer = Answer::create([
        'question_id' => $question,
        // the error undefined offset is here
        'participant_id' => $participants[$i]->id,
        'answer' => $request->participant_question[$i],
    ]);
}

异常旁边显示的行号是什么?错误消息表示
$participants
数组没有键为0的值。考虑到您是如何创建该数组的,这表明
count($request->participant\u name)
为0。您是否尝试过
var_dump($request->participant_name)
查看其中的内容?对于类似“for($i=0;$iparticipant_name);$i++)var_dump($request->participant_name);”这样的var dump,会出现“SQLSTATE[23000]:完整性约束冲突:1048列“ticket_type_id”不能为空(SQL:insert-into
参与者
姓名
姓氏
注册id
票证类型id
)值(,6,)“,”看起来肯定像你的
$request
对象中没有你期望的值。你需要了解它是如何生成的……谢谢,但foreach类似于“foreach($request->participant\u question as$key=>$question)$answer=answer::create(['question\u id'=>$question,'participant\u id'=>$participants[$i]->id,'answer'=>$request->participant\u question[$i],]))}“仍然显示”消息:“未定义的偏移量:0”。是的,我刚刚做了
问题的示例。但是我需要知道:1)参与者
来自哪里,2)他们是否真的与
问题
共享相同的键?如果2的答案是“是”,则可以使用
$participants[$key]
。对于
participant\u question
同样,如果“all\u participants”为1,则需要收集每个参与者的姓名。因此,该部分位于注册表代码中的“@if($allParticipants==1)…”中。如果用户在上一页(大会详细信息页)中选择了与票证类型相关的自定义问题,则问题也会出现在注册表中。
if (isset($request->participant_question_id[$i])) {
    $answer = Answer::create([
        'question_id' => $request->participant_question_id[$i],
        // the error undefined offset is here
        'participant_id' => $participants[$i]->id,
        'answer' => $request->participant_question[$i],
    ]);
}
foreach ($request->participant_question as $key => $question)
    $answer = Answer::create([
        'question_id' => $question,
        // the error undefined offset is here
        'participant_id' => $participants[$i]->id,
        'answer' => $request->participant_question[$i],
    ]);
}