# PHP upgrade help for each() replacement

**URL:** <https://www.phphelp.com/t/php-upgrade-help-for-each-replacement/34957>\
**Category:** Code Snippets\
**Created:** [August 20, 2022, 5:54am UTC](https://www.phphelp.com/t/php-upgrade-help-for-each-replacement/34957 "2022-08-20T05:54:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![smckinnis](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@smckinnis](https://www.phphelp.com/u/smckinnis)\
**Post date:** [August 20, 2022, 5:54am UTC](https://www.phphelp.com/t/php-upgrade-help-for-each-replacement/34957/1 "2022-08-20T05:54:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![smckinnis](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@smckinnis](https://www.phphelp.com/u/smckinnis)\
**Post date:** [August 28, 2022, 7:42pm UTC](https://www.phphelp.com/t/php-upgrade-help-for-each-replacement/34957/2 "2022-08-28T19:42:47Z")

</div>

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

> **[PHP 7.2 Deprecated each() function](https://acaroom.net/en/blog/sean/php-72-deprecated-each-function)**
>
> each() loop function is deprecated since PHP 7.2 so that you can update syntax below: Case 1: without value reset($array); while (list($key, ) = each(

### Case 1: without value

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

```

Update to

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

```

### Case 2: without key

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

```

Update to

```auto
foreach($array as $value) {

```

### Case 3: key and value

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

```

Update to

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

```

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [August 28, 2022, 8:17pm UTC](https://www.phphelp.com/t/php-upgrade-help-for-each-replacement/34957/3 "2022-08-28T20:17:54Z")

</div>

> [@smckinnis](#):
>
> `foreach($array as $value) {`

> [@smckinnis](#):
>
> foreach($lines as $params)

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