chickencommander123 Posted May 24 Share Posted May 24 Quote Link to comment https://forums.phpfreaks.com/topic/320999-whoops-undefined-array-key-how-to-define-this-array-key/ Share on other sites More sharing options...
Barand Posted May 24 Share Posted May 24 There are about a dozen array keys in that picture. If you want help... Be specific. Post textual code in a code block (<> button) and not a useless picture. 1 Quote Link to comment https://forums.phpfreaks.com/topic/320999-whoops-undefined-array-key-how-to-define-this-array-key/#findComment-1625189 Share on other sites More sharing options...
chickencommander123 Posted May 25 Author Share Posted May 25 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. Quote Link to comment https://forums.phpfreaks.com/topic/320999-whoops-undefined-array-key-how-to-define-this-array-key/#findComment-1625341 Share on other sites More sharing options...
Barand Posted May 26 Share Posted May 26 Check the content of your arrays. It looks like $allowed_forums contains a blank entry. Quote Link to comment https://forums.phpfreaks.com/topic/320999-whoops-undefined-array-key-how-to-define-this-array-key/#findComment-1625423 Share on other sites More sharing options...
Danishhafeez Posted May 29 Share Posted May 29 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 1 Quote Link to comment https://forums.phpfreaks.com/topic/320999-whoops-undefined-array-key-how-to-define-this-array-key/#findComment-1625730 Share on other sites More sharing options...
gizmola Posted June 1 Share Posted June 1 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. Quote Link to comment https://forums.phpfreaks.com/topic/320999-whoops-undefined-array-key-how-to-define-this-array-key/#findComment-1626502 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.