-1

Our host recently enforced us to upgrade to php 8.0 from 7.4

We are getting this error:

>>PHP Warning: Undefined array key.

It is a pure PHP e-commerce site originally built on the Dwoo template engine.

Can the code be modifed to work with php 8.0? And what needs to change? Below is the code:

    while (list($k, $sep) = each($m[1])) {
            if ($sep === '.' || $sep === '[' || $sep === '') {
                if ((is_array($cur) || $cur instanceof ArrayAccess) && isset($cur[$m[2][$k]])) {
                    $cur = $cur[$m[2][$k]];
                } else {
                    return null;
                }
            } elseif ($sep === '->') {
                if (is_object($cur)) {
                    $cur = $cur->$m[2][$k];
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }

        return $cur;
    }

I tried to remove the EACH function to this:

while (list($k, $sep) = $m\[1\]) {

But still receiving errors and certain things not working.

Lucas Meine
  • 1,524
  • 4
  • 23
  • 32
Heinrich
  • 1
  • 1
  • `$m[1]` probably does not exist, in that case you might want to do an early `return null` and avoid the while loop. But it's hard to tell without knowing the details. PHP should tell you on which line what error occured, so thats where you want to add a check with isset beforehand. – nimmneun Jul 19 '23 at 20:49

1 Answers1

-1

I can't give you a proper code snippet as I'm not sure which index is actually being accessed while unset, but you can use

 if( isset( $array['index blah blah'] ) {
 do stuff;
}

to check if the index is set before attempting to access it

  • 1
    Thanks for all your answers. I am still quite a php noob. Still learning. So it is a bit unclear to me. But see here for the fill code of the file called dwoo.php: http://codepad.org/WU8hyxDp The undefined array error occurs on line 1370. You help would be appreciated. – Heinrich Jul 20 '23 at 08:25