Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Chron's avatar
Level 6

Trying to access array offset on value of type null

I'm using Laravel 5.8 with php 7.4.

The error points at line 147 of /project/vendor/egulias/email-validator/EmailValidator/Parser/Parser.php

    protected function escaped()
    {
        $previous = $this->lexer->getPrevious();
 
 >>>>  if ($previous['type'] === EmailLexer::S_BACKSLASH
            &&
            $this->lexer->token['type'] !== EmailLexer::GENERIC
        ) {
            return true;
        }
 
        return false;
    }

Is there a way to fix this without downgrading php or upgrading laravel?

0 likes
16 replies
fylzero's avatar

@chron Why not just upgrade Laravel? 5.8 and 6.15.1 are not much different. Keep in mind going from v5 to v6 was just Laravel moving to semver. It is not a massive upgrade.

Pretty much open your composer.json and bump the "laravel/framework": "^6.0" up. I would remove filp/whoops and replace that with "facade/ignition": "^1.4",. Then run composer require laravel/helpers to add back the string and array helpers in case any packages use that.

Run composer update. Then regression test your app (basically make sure everything still works). In all likelihood this will just work. If you hit any snags, consult the upgrade guide.

https://laravel.com/docs/6.x/upgrade

Chron's avatar
Chron
OP
Best Answer
Level 6

I just edited the required version of php in composer.json to ^7.4.1 and it worked. No need to update laravel and downgrade php since my OS already has installed php v7.4.1.

5 likes
coedycode's avatar

In case this trips anyone else up. I was running 5.8 with php7.3-fpm and still seeing this error in the logs, but all for the queued jobs. Turns out Ubuntu had done an upgrade to php 7.4 and so the default php version for the queue:workers also became 7.4. Solution: specify the version of php in the supervisord config that starts the queue workers, e.g.

command=/usr/bin/php7.3 artisan queue:work ...

claudios's avatar

@chron did the same but no luck. I think there's another culprit behind this error.

AnnetteV's avatar

@chron. I tried it, too. But without luck.

4 days later: I upgraded Laravel to 6.0 or higher, then it works

ThinkingMan's avatar

Seeing some working arounds on this issue but is there a approach to resolve the array? So I get the error because of the segment "$mc_lists = collect($mc_lists['lists'])->pluck('name', 'id');" in the code below.

$settings = MerchantSetting::get();

        $mc_service = new MailchimpService(auth()->user());
        $mc_valid   = $mc_service->isValidToken;
        $mc_lists   = $mc_valid ? $mc_service->getLists() : null;
        $mc_lists   = collect($mc_lists['lists'])->pluck('name', 'id');

        return view('configurator.configurator')->with([
            'getswift_key'   => $settings->where('slug', MerchantSetting::GETSWIFT_KEY_SLUG)->first(),
            'order_code'     => $settings->where('slug', MerchantSetting::MERCHANT_ORDER_CODE_SLUG)->first(),
            'mc_customer'    => $settings->where('slug', MerchantSetting::MAILCHIMP_CUSTOMERS_LIST_SLUG)->first(),
            'mc_transaction' => $settings->where('slug', MerchantSetting::MAILCHIMP_TRANSACTIONS_LIST_SLUG)->first(),
            'mc_valid'       => $mc_valid,
            'mc_lists'       => $mc_lists,

I saw @snapey posted a solution to catch the array on this post https://laracasts.com/discuss/channels/laravel/trying-to-access-array-offset-on-value-of-type-null but not sure if that would work here.

GuntarV's avatar

After upgrading PHP to 7.4, I was also getting Trying to access array offset on value of type null error.

Error was coming from second line in code block below:

$row = $this->nextCuttingCollection($k,$emptyCount,$prevCutting,$field);
$cuttingArray[$k][$row['key']] = $row['data'];

Honestly I don't even know how it worked in previous PHP versions, but it did. Even in previous PHP versions the $row was null, but it never thrown an error on second line, but in 7.4 version it did, which is how it should be. I think this error is PHP related not Laravel.

ufukcam's avatar

I changed php version 7.1 then my problem solved.

prakashbhatnager@gmail.com's avatar

In my case i have just updated code In line 147 of this file vendor\egulias\email-validator\EmailValidator\Parser\Parser.php

i have just updated it as

Previous code was if ($previous['type'] === EmailLexer::S_BACKSLASH

New code if (isset($previous['type']) && $previous['type'] === EmailLexer::S_BACKSLASH

and my problem solved.

2 likes
iamjayu9's avatar

You save me, man that's working solutions, thanks a lot

Please or to participate in this conversation.