PHP upgrade help for each() replacement

I am trying to upgrade php 7.4 to php 8.0. One popular code in my script is the each($array) line, now depreciated. I have most taken care of most but one is throwing me, should be simple.

Most are like:
while(list($key, $params) = each($lines))
which can be recoded as
foreach($lines as $key => $params)
but this one is
while(list(, $params) = each($lines))
So the key is not specified, I assume this just reads in each line,
if I code it like below, I get an error.
foreach($lines as $params)

What am I missing?

Thought I would get a response but did not. I finally find this online

Case 1: without value

reset($array);
while (list($key, ) = each($array)) {

Update to

foreach(array_keys($array) as $key) {

Case 2: without key

reset($array);
while (list(, $value) = each($array)) {

Update to

foreach($array as $value) {

Case 3: key and value

reset($array);
while (list($key, $value) = each($array)) {

Update to

foreach($array as $key => $value) {

And that’s what you did, but you stated you got an error, which you didn’t post for anyone here to help with.

Sponsor our Newsletter | Privacy Policy | Terms of Service