Title: Undefined array key 1 in PHP 8
Last modified: April 17, 2021

---

# Undefined array key 1 in PHP 8

 *  Resolved [Knightycool](https://wordpress.org/support/users/knightycool/)
 * (@knightycool)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/)
 * I get this warning after upgrading to php 8.0.3
 * [17-Apr-2021 09:38:57 UTC] PHP Warning: Undefined array key 1 in /var/www/mywebsite/
   wp-content/plugins/autoptimize/classes/autoptimizeStyles.php on line 275

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/page/2/?output_format=md)

 *  Plugin Author [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14330646)
 * Hmm … [The code runs a regular expression if `media=` is found and the regex _should_ return the attribute value in `$medias[1]`](https://github.com/futtta/autoptimize/blob/2.8.3/classes/autoptimizeStyles.php#L273-L275),
   so it looks like somehow that regex is not (always) returning an attribute value.
 * Can you check the HTML source of a couple of pages on your site and tell me what
   different media attribute-values are there for your CSS?
 *  Thread Starter [Knightycool](https://wordpress.org/support/users/knightycool/)
 * (@knightycool)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14330747)
 * itemprop, viewbox (for svg),
 *  Thread Starter [Knightycool](https://wordpress.org/support/users/knightycool/)
 * (@knightycool)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14330765)
 * I added this line up to the original line:
 * error_log(print_r($medias[1],true));
    $medias = explode( ‘,’, $medias[1] );
 * And i’ll check the error log.
 *  Thread Starter [Knightycool](https://wordpress.org/support/users/knightycool/)
 * (@knightycool)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14330818)
 * ok here is an update
 * Normally the entry in the error log is like this:
 * [17-Apr-2021 12:31:52 UTC] Array
    ( [0] => media=’all’ [1] => all )`
 * BUT sometimes it becomes this:
 * 17-Apr-2021 12:32:22 UTC] Array
    ( )
 * [17-Apr-2021 12:32:22 UTC] PHP Warning: Undefined array key 1 in /var/www/mywebsite/
   wp-content/plugins/autoptimize/classes/autoptimizeStyles.php on line 276`
    -  This reply was modified 5 years, 1 month ago by [Knightycool](https://wordpress.org/support/users/knightycool/).
    -  This reply was modified 5 years, 1 month ago by [Knightycool](https://wordpress.org/support/users/knightycool/).
 *  Thread Starter [Knightycool](https://wordpress.org/support/users/knightycool/)
 * (@knightycool)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14330828)
 * Is there a way to learn in which post this happens?
 *  Thread Starter [Knightycool](https://wordpress.org/support/users/knightycool/)
 * (@knightycool)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14330913)
 * Ok i get it now. I added the id number and could pinpoint where it happened…
 * In the custom css of a single page (where it happens), there is a comment like
   this:
 * /*! CSS Used from: [https://www.mysite.net/wp-content/myapp.css](https://www.mysite.net/wp-content/myapp.css);
   media=all */
 * So it matches media=all
 * I removed it and try to see if the warning happens again. Perhaps you may find
   a way to capture media=all only if its between certain html tags.
 *  Plugin Author [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14331062)
 * that’s unlikely to be the culprit; the code I referenced earlier is only applied
   on `<style` and `<link rel=stylesheet` tags, see [https://github.com/futtta/autoptimize/blob/2.8.3/classes/autoptimizeStyles.php#L266](https://github.com/futtta/autoptimize/blob/2.8.3/classes/autoptimizeStyles.php#L266)…:-/
 * could you share the URL of the page/post with the id so I can have a look at 
   the HTML source?
 *  Thread Starter [Knightycool](https://wordpress.org/support/users/knightycool/)
 * (@knightycool)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14331076)
 * It was the reason because the said comment was inside <style> </style>
 * <style>
    .myclass { font-size: 10px; background-color: red; } .myclass2 { height:
   100px; width: 100px; } /*! CSS Used from: [https://www.mysite.net/wp-content/myapp.css](https://www.mysite.net/wp-content/myapp.css);
   media=all */ .myclass3 { border-color: black; } </style>
 *  Plugin Author [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14331083)
 * OK, I’ll test with that code locally, more soon 🙂
 *  Thread Starter [Knightycool](https://wordpress.org/support/users/knightycool/)
 * (@knightycool)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14331087)
 * If you want, i can add back the comment and email you the url but i dont want
   to post the url here.
 *  Plugin Author [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14331612)
 * OK, I have a fix in AO beta, could you download from [https://github.com/futtta/autoptimize/archive/refs/heads/beta.zip](https://github.com/futtta/autoptimize/archive/refs/heads/beta.zip),
   install that instead of 2.8.3 and test if that fixes the notice?
 * (the beta has some other new features already; per page/post settings for AO 
   + new “defer inline JS” option)
 *  Thread Starter [Knightycool](https://wordpress.org/support/users/knightycool/)
 * (@knightycool)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14331615)
 * umm ok, the notice has already disappeared but i will re enter the comment and
   then install the beta and try to see what happens.
 *  Thread Starter [Knightycool](https://wordpress.org/support/users/knightycool/)
 * (@knightycool)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14331630)
 * Ok. This is what i did:
 * 1) Added back the comment line.
 * 2) Check to see the warning happens. And it happened when i visit the post.
 * 3) I installed beta
 * 4) Visited the post and there is no warning. I think it is fixed.
 * Thanks a lot. What have you changed exactly? May you show me line? And when a
   new update comes, i can just update this via wordpress right?
 *  Plugin Author [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14331650)
 * Great!
 * this is the change: [https://github.com/futtta/autoptimize/commit/37b13d4e19269bb2f50df123257de51afa37244f](https://github.com/futtta/autoptimize/commit/37b13d4e19269bb2f50df123257de51afa37244f)
 * And indeed when 2.9 is released this will be in it. If you keep the beta active
   you will receive updates of that as well by the way, so you can keep track of
   both 😉
 *  Thread Starter [Knightycool](https://wordpress.org/support/users/knightycool/)
 * (@knightycool)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/#post-14331660)
 * Thanks a lot. You are great. Loves from Turkey..

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/page/2/?output_format=md)

The topic ‘Undefined array key 1 in PHP 8’ is closed to new replies.

 * ![](https://ps.w.org/autoptimize/assets/icon-256X256.png?rev=2211608)
 * [Autoptimize](https://wordpress.org/plugins/autoptimize/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/autoptimize/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/autoptimize/)
 * [Active Topics](https://wordpress.org/support/plugin/autoptimize/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/autoptimize/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/autoptimize/reviews/)

 * 16 replies
 * 2 participants
 * Last reply from: [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * Last activity: [5 years, 1 month ago](https://wordpress.org/support/topic/undefined-array-key-1-in-php-8/page/2/#post-14331668)
 * Status: resolved