Jump to content

whoops! undefined array key, how to define this array key?


Recommended Posts

The error is coming from line 347. The line containing $f = $forums['f'][$forum_id];. The message showed up when I upgraded to PHP 8.3.7 from 8.3.6. before then, there were no errors. so I'm guessing it's a deprecation error. I will try downgrading to 8.3.6. the error message reads vaguely;

Undefined array key ""

I changed the single quotations to double quotations and still it remains.

Link to comment
Share on other sites

Since PHP 8.3 may have stricter error reporting or handling compared to 8.3.6, this issue might have been silently ignored or handled differently in the previous version.

To fix this issue, you should ensure that $forum_id is always set and not an empty string before trying to access $forums['f'][$forum_id]. You can also add a check to make sure the key exists in the array.

foreach ($allowed forums as $forum_id) {
    if (empty($forum_id)) {
        // Skip this iteration if $forum_id is empty
        continue;
    }

    if (isset($forums['f'][$forum_id])) {
        $f = $forums['f'][$forum_id];
    } else {
        // Handle the case where $forum_id is not found in $forums['f']
        $f = []; // or set a default value, or log an error
    }

    $cat_forum['c'][$f['cat_id']][] = $forum_id;

    if ($f['forum_parent']) {
        $cat_forum['subforums'][$forum_id] = true;
        $cat_forum['forums_with_sf'][$f['forum_parent']] = true;
    }
}

Check for the existence of the array key using array_key_exists():

foreach ($allowed forums as $forum_id) {
    if (empty($forum_id)) {
        // Skip this iteration if $forum_id is empty
        continue;
    }

    if (array_key_exists($forum_id, $forums['f'])) {
        $f = $forums['f'][$forum_id];
    } else {
        // Handle the case where $forum_id is not found in $forums['f']
        $f = []; // or set a default value, or log an error
    }

    $cat_forum['c'][$f['cat_id']][] = $forum_id;

    if ($f['forum_parent']) {
        $cat_forum['subforums'][$forum_id] = true;
        $cat_forum['forums_with_sf'][$f['forum_parent']] = true;
    }
}

Check for Empty $forum_id: Ensuring $forum_id is not empty helps prevent accessing array keys with empty values.

isset() and array_key_exists(): Both functions ensure that the key exists in the array before accessing it. isset() also checks that the value is not null, whereas array_key_exists() strictly checks for the presence of the key regardless of its value.

Best Regard

Danish Hafeez | QA Assistant

ICTInnovations

  • Like 1
Link to comment
Share on other sites

On 5/26/2024 at 1:15 AM, Barand said:

Check the content of your arrays.

It looks like $allowed_forums contains a blank entry.

This is the answer.  If you log out the $allowed_forums array, or even just the $forum_id inside the foreach loop, you will see that at least one entry is blank/empty.  Danish provided at least 2 ways you could rewrite the code to mitigate this.  You also might look into how exactly it is that the array gets built with empty array elements in the first place, and prevent that from happening.  There are useful performant array functions like array_filter that can make this quite easy to do.

 

For example:

foreach (array_filter($allowed_forums) as $forum_id) {
... etc.
}

By default, with no callback, array_filter will remove any array elements that are empty or false equivalent, so that would also fix your current issue, although again, it might be better to find the actual source of the empty/erroneous array value in the first place.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.