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

时间:2015-12-10 09:35:01

标签: php arrays

以下是代码:

        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 Undefined offset:2。我真的不确定是什么导致了这个。

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

1 个答案:

答案 0 :(得分:0)

通常情况下,尝试访问不存在的数组密钥会触发Undefined offset消息。

您的代码示例似乎实际上并未显示触发错误的部分...在您的情况下,我希望代码显示类似$var[2]的内容,其中数组实际上不包含索引2

请参阅this other question以更清楚地表示该错误的含义。