Causes and solutions for the warning “Warning: Undefined array key “〇〇” in…”

PubDate:

/ ModDate:

WordPressのトラブル対処方法の画像

<Description based on the Act on Premiums and Representations> The content on this site may include product promotions.



The warning “Warning: Undefined array key “〇〇” in…” is displayed when debug mode is turned on (true). When you get an array of something, there are keys that don’t exist! This is a warning.

It doesn’t occur until this situation actually occurs, so it was fine until now even though we had been operating with the same code for a long time. .. It is also a feature that it often happens.

Also, since this warning is based on rules that have become stricter since PHP 8.0, it will suddenly occur after changing the PHP version, so please be aware that this warning will occur suddenly if the PHP version is updated automatically. Then suddenly… .. You may feel that way.

Since it is a warning display, it will not immediately stop the program (site), so one way is to stop the display, but as the PHP version increases, it will become more strict, so do it as soon as possible. It might be better to deal with it.

General response to “Warning: Undefined array key “〇〇” in…”

The solution is to “exclude undefined items”, so in most cases you can use isset to determine whether it exists (if you need to take a different approach depending on the code) ).

in particular,

$ua = $_SERVER['HTTP_USER_AGENT'];

When you try to get a user agent and store it in a variable called $ua, a warning will be displayed if there is an undefined user agent, so make sure to pass the value to $ua as shown below. This will solve the problem.

  1. Default value for $ua (set to ” (empty) in the code example)
  2. Determine if $_SERVER[‘HTTP_USER_AGENT’] exists with isset
  3. If 2 is true, set $_SERVER[‘HTTP_USER_AGENT’] in $ua
$ua = '';

if(isset($_SERVER['HTTP_USER_AGENT'])){
	$ua = $_SERVER['HTTP_USER_AGENT'];
}

This is due to stricter handling of empty and undefined items in PHP 8.0, so if this occurs with a plugin you are using, we will notify the author, or if you create your own plugin or copy it from somewhere. If you have implemented the code, you can carefully look at the relevant code and modify it.

However, in the case of code like the example, the $ua variable is often specified in order to perform another conditional branch, so it may be necessary to carefully examine what is being done in the subsequent branch. There is a possibility that you will get an incorrect result, so please check the actual code when setting the default value.

QA Analytics QA Analytics

Specific example of processing to update custom field value from meta box

When creating a meta box (input window) to set the value of a custom field in a post or fixed page, and updating the value entered there, a warning “Warning: Undefined array key “〇〇” in…” appears. You may.

Below is an example of the code I actually dealt with. I think this process is probably done using similar code, so I hope this helps.

Code example that worked in versions earlier than PHP 8.0

For the meta key “is_nofollow”,

  • If there is an input in the meta box but there is no custom field value, update with the entered value
  • If there is no input in the meta box and there is a custom field value, delete the custom field.

This process is performed. The following description was fine until PHP8.0.

//Nofollow update
if(!empty($_POST['is_nofollow'])){
	update_post_meta($post_id, 'is_nofollow', $_POST['is_nofollow'] );
}else{
	delete_post_meta($post_id, 'is_nofollow', $_POST['is_nofollow']);
}

Code example that is compatible with PHP 8.0 and later versions

The cause of “Warning: Undefined array key “〇〇” in…” is that the key is not defined in the array, so use isset to determine whether the key exists.

Determining the existence of a key

If the key “$_POST[‘is_nofollow’]” exists, it is assumed that nothing happened for that key. .. Make this judgment and store it in a variable called $is_nofollow (determine whether it exists or not)

if(isset($_POST['is_nofollow'])){
	$is_nofollow = $_POST['is_nofollow'];
}else{
	$is_nofollow = '';
}

Update processing changes

For $is_nofollow (if there is a meta key, that meta key, otherwise empty), if there is a value entered in the meta box, it will be updated, otherwise it will be deleted.

if(!empty($is_nofollow)){
	update_post_meta($post_id, 'is_nofollow', $is_nofollow );
}else{
	delete_post_meta($post_id, 'is_nofollow', $is_nofollow);
}

By combining both, you get the following set.

if(isset($_POST['is_nofollow'])){
	$is_nofollow = $_POST['is_nofollow'];
}else{
	$is_nofollow = '';
}

if(!empty($is_nofollow)){
	update_post_meta($post_id, 'is_nofollow', $is_nofollow );
}else{
	delete_post_meta($post_id, 'is_nofollow', $is_nofollow);
}

There is also a way to have the entire process judged by isset. .. ..

You can also write it as follows without separating the processing.

if(isset($_POST['is_nofollow'])){
	if(!empty($_POST['is_nofollow'])){
		update_post_meta($post_id, 'is_nofollow', $_POST['is_nofollow'] );
	}else{
		delete_post_meta($post_id, 'is_nofollow', $_POST['is_nofollow']);
	}
}

Even with this, the PHP warning itself will no longer appear.

However, although the above code works fine when inputting text, the deletion process is not performed for checkboxes, select boxes, radio buttons, etc., and even though I unchecked them, when I updated them, they remained checked. Please make sure to actually test it and make sure it is processed properly.


There are many other cases where warnings like this occur when switching to PHP 8.0 or later, but all of them are due to PHP 8.0 becoming stricter about “reliably writing processing in the case that there is no such thing” Therefore, if a warning occurs, it is okay to deal with it by first checking whether abnormalities are handled properly when using if(…).

Of course, don’t forget to check and test after rewriting the program!


Lastly, as a side note, I was using WordPress normally and suddenly this message appeared! I have to do something! If you are thinking that, the problem may be that the message appears in the first place, so please refer to the following page.


This page is an English translation of the Japanese page. The information on the Japanese page may be more accurate because we maintain the Japanese page as the basis and not all content matches.
You can view the Japanese page by clicking the “View Japanese Page” button on the top right of the page, so please take a look.

If you found the content posted on this page or the reference code introduced helpful, please leave a comment in the comment section at the end of the page, or spread the word on SNS etc.

The methods and codes posted on this page have been confirmed to work in my environment as of the last update date. Please note that in principle, we will not respond even if we receive information about malfunctions or detailed customization methods in the comments section.

Until the end Thank you for reading.

The copyright and ownership of all content published on this page, including texts, images, and codes, belongs to this site, and reproduction is strictly prohibited.