109

With PHP 7.2, each is deprecated. The documentation says:

Warning This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.

How can I update my code to avoid using it? Here are some examples:

  1. $ar = $o->me;
    reset($ar);
    list($typ, $val) = each($ar);
    
  2. $out = array('me' => array(), 'mytype' => 2, '_php_class' => null);
    $expected = each($out);
    
  3. for(reset($broken);$kv = each($broken);) {...}
    
  4. list(, $this->result) = each($this->cache_data);
    
  5. // iterating to the end of an array or a limit > the length of the array
    $i = 0;
    reset($array);
    while( (list($id, $item) = each($array)) || $i < 30 ) {
        // code
        $i++;
    }
    

When I execute the code on PHP 7.2 I receive the following error:

Deprecated: The each() function is deprecated. This message will be suppressed on further calls

TylerH
  • 20,799
  • 66
  • 75
  • 101
yokogeri
  • 1,217
  • 2
  • 8
  • 11

12 Answers12

98
  1. For your first two example cases, you could use key() and current() to assign the values you need.

    $ar = $o->me;   // reset isn't necessary, since you just created the array
    $typ = key($ar);
    $val = current($ar);
    
  2. $out = array('me' => array(), 'mytype' => 2, '_php_class' => null);
    $expected = [key($out), current($out)];
    

    In those cases, you can use next() to advance the cursor afterward, but it may not be necessary if the rest of your code doesn't depend on that.

  3. For the third case, I'd suggest just using a foreach() loop instead and assigning $kv inside the loop.

    foreach ($broken as $k => $v) {
         $kv = [$k, $v];
    }
    
  4. For the fourth case, it looks like the key is disregarded in list(), so you can assign the current value.

    $this->result = current($this->cache_data);
    

    Like the first two cases, it may be necessary to advance the cursor with next() depending on how the rest of your code interacts with $this->cache_data.

  5. Fifth can be replaced with a for() loop.

    reset($array);
    for ($i = 0; $i < 30; $i++) {
        $id = key($array);
        $item = current($array);
        // code
        next($array);
    }
    
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • 2
    For 4., I think it's right to replace `list($a, $b) = each($arr)` by `list($a, $b) = array(key($arr), current($arr)); next($arr);` isn't it ? – Metal3d Nov 03 '18 at 12:52
  • @Metal3d yes, that should be equivalent. Although personally, I wouldn't use list, I would just assign to $a and $b directly with key() and current(). I know it's one more line of code, but it seems more straightforward than creating an array just to take the values back out with list(). Just my opinion, though. :-) – Don't Panic Nov 03 '18 at 14:27
  • See universal automated migration version bellow: https://stackoverflow.com/a/55514591/1348344 – Tomas Votruba Jan 18 '20 at 15:08
  • For case 1 I believe you need to make sure internal pointer is advanced after calling current() since it doesn't move the pointer. – charitha Jun 06 '20 at 10:50
60

2019+ Instant Upgrade of each()

There are actually plenty of cases that each() can be replaced, that's why there are so many different upvoted answers in this question.

-while (list($key, $callback) = each($callbacks)) {
+foreach ($callbacks as $key => $callback) {
     // ...
 }

And:

-while (list($key) = each($callbacks)) {
+foreach (array_keys($callbacks) as $key) {
     // ...
 }

You can replace one by one manually. But isn't there a better way?

I help to migrate projects, where are over 150+ cases like this. I'm lazy so I made a tool called Rector, that converts the code the way above (+ there are more cases, but I don't want to spam the answer).

It's part of the PHP_72 set.


4 Steps to Upgrade your Code

1. Install it

composer require rector/rector --dev

2. Create rector.php config

vendor/bin/rector init

3. Add PHP_72 set

<?php

use Rector\Core\Configuration\Option;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    $parameters->set(Option::SETS, [
        Setlist::PHP_72,
    ]);
};

4. Run it on your code

vendor/bin/rector process src --set php72

I hope it helps you with your migration.


If there is some bug or anomaly, it's Rector missed case. Create an issue, so we can fix it and make it work for every case possible.

Tomas Votruba
  • 23,240
  • 9
  • 79
  • 115
  • 1
    The last example using key() and current() is correct value-wise but disregards the fact that each() also advances the array cursor as a side-effect. Also, you probably mean $val and not $callback in the list() call. A proper replacement would be: -list($key, $val) = each($callbacks); +$key = key($opt->option); +$val = current($opt->option); +next($callbacks); – Nils Oct 31 '19 at 09:40
  • Could you create an issue for this so it's fixed? https://github.com/rectorphp/rector/issues – Tomas Votruba Nov 01 '19 at 09:30
  • 2
    I'm not using that library, I was just googling for an each() replacement, came across your post here and found it useful but just thought I'd point out that small omission so you could correct your post. – Nils Nov 03 '19 at 20:16
  • I see. Still always better address that in Github repo issue. Rarely maintainers visit their old responses and the bug usually hits more people – Tomas Votruba Nov 03 '19 at 23:25
  • @Nils I've updated the example. It's very hard to read from the inlined code as text comment, https://gist.github.com/ woudl be better. Could you check? – Tomas Votruba May 14 '20 at 17:21
  • You added next() which takes care of advancing the pointer but still need to use the same variable names throughout ($val vs $callback) if the examples should be equivalent. See https://3v4l.org/DWYMt for full code examples and tests proving the results. – Nils May 16 '20 at 11:41
  • I get lost in so many lines of code. Could you give me only the content to past there? – Tomas Votruba May 16 '20 at 22:57
  • I've given you the complete answer twice already. If you're not interested in spending the one minute it takes to fully understand where your answer goes wrong, I leave it up to you. If you just re-read your solution it's pretty obvious since you do not even use the same variable names... – Nils May 18 '20 at 06:09
  • Sorry, it's just not clear to me and this is just example answer. Any code could be there. The point is all the changes should be automated, exactly for this confusion. I'll drop the snippet, no to lead anyone astrey. – Tomas Votruba May 18 '20 at 11:53
47

I found a way to fix it and thought to share the information. Here are also other cases about how to upgrade each() loops to foreach().

Case 1: Missing $value

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

Update to:

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

Case 2: Missing $key

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

Update to:

foreach($array as $value) {

Case 3: Not missing anything

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

Update to:

foreach($array as $key => $value) {
Petro Mäntylä
  • 711
  • 1
  • 7
  • 9
38

you could create your own each() function using key(), current() and next(). then replace your calls with that function, like this:

<?php
function myEach(&$arr) {
    $key = key($arr);
    $result = ($key === null) ? false : [$key, current($arr), 'key' => $key, 'value' => current($arr)];
    next($arr);
    return $result;
}

1.

$ar = $o->me;
reset($ar);
list($typ, $val) = myEach($ar);

2.

$out = array('me' => array(), 'mytype' => 2, '_php_class' => null);
$expected = myEach($out);

3.

for(reset($broken);$kv = myEach($broken);) {...}
Wee Zel
  • 1,294
  • 9
  • 11
  • If you want to fully emulate each, I guess you'd need the "key" and "value" keys in the output as well as 0 and 1. – Don't Panic Sep 29 '17 at 16:31
  • 1
    @Don'tPanic, edited answer, this situation didn't need it but there could be cases out there that might. thanks for suggestion – Wee Zel Sep 29 '17 at 16:41
11
reset($array);
while (list($key, $value) = each($array)) {

UPDATE

reset($array);
foreach($array as $key => $value) {
Stewen Guyon
  • 119
  • 1
  • 2
  • 7
    Important to note that these are not equivalent, although in most cases a foreach will suffice – if you modify `$array` in the while loop it will iterate over the modified values. `foreach` creates a copy of the list and iterates over it, so mutations to `$array` will not change the loop. – jpschroeder Oct 30 '18 at 02:45
  • 1
    @jpschroeder good point, that's true. Also, with foreach, reset is not necessary. – Don't Panic Nov 03 '18 at 14:36
  • The reset is mostly usless before foreach. – FrancescoMM Oct 31 '19 at 07:14
  • That's completely different function... cannot be used in recursions – Martin Zvarík Oct 31 '20 at 22:06
5

Here are some ways to do it:

The standard foreach loop (very readable):

foreach($this->contents as list($products_id)) {
    $total_items += $this->get_quantity($products_id);
}

Or, reducing:

$total_items = array_reduce($this->contents, function($acc, $item) {
    return $acc + $this->get_quantity($products_id[0]);
});

Or, in a functional expression:

$total_items = array_sum(array_map([$this, 'get_quantity'],
                         array_column($this->contents, 0)));

None of these methods need reset($this->contents); preceding it.

trincot
  • 317,000
  • 35
  • 244
  • 286
5

The way you most definitely shouldn't do is put the function "back into php" by adding it to the auto_prepend_file setting in php.ini

auto_prepend_file = "/var/www/php/auto_prepend.php"

Then make the file and enter in the function with an function_exists wrapper.

<?php
/**
 * Adds the depreciated each() function back into 7.2
 */
if (!function_exists('each')) {
    function each($arr) {
        $key = key($arr);
        $result = ($key === null) ? false : [$key, current($arr), 'key' => $key, 'value' => current($arr)];
        next($arr);
        return $result;
    }
}

This essentially declares the function before your php application runs. When your application tries to run the each function it'll use your version instead.

This is absolutely not the way you should be approaching this problem, especially in production! However you're a developer with time constraints and you just want to try arbitrary frameworks for your next project and they haven't been updated to work on your local development server without winding back your php version.

When you've committed to a code base for your project please go ahead and implement the changes in the accepted answer because they do work.

I used Wee Zel's emulation of the each function

  • 1
    The replacement function runs into an endless loop in my case. probably because it doesent take into account `reset()` and `next()` – rubo77 Oct 02 '20 at 09:17
1

To expand on Petro Mäntylä excellent correct answer for Case 3:

Here is a full example of a "Case 3" situation, because I find full examples far more informative that one line code fragments:

This is genuine code from a 3rd party old code base (TCPDF)

DEPRECATED:

while (list($id, $name) = each($attr_array)) {
      $dom[$key]['attribute'][$name] = $attr_array[$id];
      ...              
      ...             
   }

FIXED:

 // while (list($id, $name) = each($attr_array)) {
 foreach($attr_array as $feKey => $feRow){
    // $dom[$key]['attribute'][$name] = $attr_array[$id];
    $dom[$key]['attribute'][$feRow] = $attr_array[$feKey];
    ...
    ...
    }
 unset($feKey,$feRow);
Martin
  • 22,212
  • 11
  • 70
  • 132
0

Replace this code

while (list($_key,$_resourceTypeNode) = each($GLOBALS['config']['ResourceType'])) {
//             if ($_resourceTypeNode['name'] === $resourceTypeName) {
//                 $this->_resourceTypeConfigCache[$resourceTypeName] = new CKFinder_Connector_Core_ResourceTypeConfig($_resourceTypeNode);

//                 return $this->_resourceTypeConfigCache[$resourceTypeName];
//             }
//         }

with this one

foreach ($GLOBALS['config']['ResourceType'] as $key => $_resourceTypeNode) {
            if (isset($_resourceTypeNode['name'])) {
                if ($_resourceTypeNode['name'] === $resourceTypeName) {
                    $this->_resourceTypeConfigCache[$resourceTypeName] = new CKFinder_Connector_Core_ResourceTypeConfig($_resourceTypeNode);
                    
                    return $this->_resourceTypeConfigCache[$resourceTypeName];
                }
            }
        }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Sahil
  • 9
  • 1
-1
 //  while (list($products_id, ) = each($this->contents)) {
   //  $total_items += $this->get_quantity($products_id);
 // }

Update To :

foreach(array_keys($this->contents) as $products_id) {
  $total_items += $this->get_quantity($products_id);
}

Other Condition:

foreach($this->contents as $key =>$value) {
  $total_items += $this->get_quantity($products_id);
}
Harendra Singh
  • 119
  • 1
  • 9
-1

For all commentators. Function foreach not work with dinamic arrays with changing of elements count. I think using custom function "each" John Tilley - single right way for dinamic arrays. For static arrays not use "each" nobody. "Foreach" know all.

  • 1
    Tried to improve the post, but like mostly always, "the edit queue is full at the moment - try again in a few minutes!" – Ingo Steinke Aug 17 '22 at 08:41
-3

What about using this function?

function array_fetch(array $a) {
   $element = current($a);
   next($a);
   return $element;
}
garsax
  • 163
  • 1
  • 5