0

What does the error mean and how do I fix it?

( Notice: Trying to access array offset on value of type null in....)

public function hasPermission($key)
{
    $group = $this->_db->get('groups', array('id', '=', $this->data()->group));
    if ($group->count()) {
        $permissions = json_decode($group->firstResult()->permissions, true);

        if ($permissions[$key] == true) {
            return true;
        }
    }

    return false;
}

The error is on this line:

if ($permissions[$key] == true) {

Regards

2
  • This question is already answered, example at stackoverflow.com/questions/59336951/…. Commented Aug 16, 2020 at 16:16
  • Are you sure the value for $key is available in the array? Commented Aug 16, 2020 at 16:25

2 Answers 2

1

You don't need to do

if ($permissions[$key] == true) {
     return true;
}

Rather try this:

if (isset($permissions[$key]) && $permissions[$key]) {
    return true;
}

Hope this works!!

Sign up to request clarification or add additional context in comments.

Comments

-1

It means that you're trying to get data from you're array while the parameter returns NULL in $permissions[$key]. So check if you're $key has any value.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.