获取未定义的偏移量:foreach循环出现2个错误


Getting Undefined offset: 2 error on foreach loop

以下是代码:

        foreach ($test_scores as $score) {
        switch ($score['name']) {
            case 'LE-TOD':
                $le_tod = $score['banding'];
                $le_tod_desc = $score["result_desc"];
                if($score['std_score'] == 0) {
                    $le_tod_small = $scoring['tod']['no_pref_bottom'];
                }
                else {
                    $le_tod_small = $scoring['tod']['pref_bottom'].$score['banding']; 
                }
                break;

这就是我得到错误消息的地方:

    public static function generateLSPReportFile($dataInput, $auto_download = true)
{
    $file_type = $dataInput['file_type'];
    $test_type_id = $dataInput['test_type_id'];
    $he_she = array('male' => 'He', 'female' => 'She');
    $his_her = array('male' => 'his', 'female' => 'her');
    $candidate = 'Candidate::where('id',$dataInput['candidate_id'])->first();
    if(!$candidate)
        return;
    $candidate = $candidate->toArray();
    $candidate_name = $candidate['first_name'].' '.$candidate['last_name'];
    $order = 'Order::with('client')->find($dataInput['order_id']);
    if(!$order)
        return;
    $client = $order->client;
    $order = $order->toArray();
    //for test dates
    $order_candidate = 'OrderCandidate::candidateId($dataInput['candidate_id'])->orderId($dataInput['order_id'])->first()->toArray();
    $order_tests = 'OrderCandidateTest::orderCandidateId($order_candidate['id'])
                        ->status('Config::get('kcg.candidate_test_status_taken'))
                        ->testId($dataInput['test_id'])
                        ->with(array('test' => function($query){
                            $query->select(['id', 'name', 'description', 'abbreviation']);
                        }));
    $tests_scores = 'OrderCandidateTestScore::orderId($dataInput['order_id'])
                        ->candidateId($dataInput['candidate_id'])
                        ->testId($dataInput['test_id'])
                        ->with(array('test' => function($query){
                            $query->select(['id', 'name', 'description', 'test_type_id']);
                        }));
    $test_type = TestType::find($test_type_id);
    $test_scores = $tests_scores->orderBy('sequence_no')->get()->toArray();
    $order_tests = $order_tests->get()->toArray();
    if(!$order_tests)
        return;
    if(!$tests_scores)
        return;

它发布了一个错误:ErrorException未定义的偏移量:2。我真的不确定是什么原因造成的。

我在这里做错了什么?请帮帮我。谢谢

通常,Undefined offset消息将由尝试访问不存在的数组键触发

看起来您的代码示例实际上可能没有显示触发错误的部分。。。在您的情况下,我希望代码显示类似$var[2]的内容,其中数组实际上不包含2的索引。

请参阅另一个问题,以更清楚地表示该错误的含义。