How to enable this deprecated function every php

PHP 7.2 is eachdeprecated. The documentation says:

Warning This function has been deprecated since PHP 7.2.0. Relying on this feature 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++;
    }
    
+36
source share
7 answers

In the first two examples, you can use key()and current()to assign the necessary values.

  1. $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 these cases, you can use next()to move the cursor later, but this may not be necessary if the rest of your code does not depend on this.

  1. foreach() $kv .
    foreach ($broken as $k => $v) {
        $kv = [$k, $v];
    }
  1. , list(), .
    $this->result = current($this->cache_data);

, $this->cache_data next() , $this->cache_data.

  1. for() for().
    reset($array);
    for ($i = 0; $i < 30; $i++) {
        $id = key($array);
        $item = current($array);
        // code
        next($array);
    }
+37

each() key(), current() next(). , :

<?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);) {...}
+19
reset($array);
while (list($key, $value) = each($array)) {

reset($array);
foreach($array as $key => $value) {
+7

2019+ each()

, each() , .

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

:

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

:

-list($key, $callback) = each($callbacks);
+$key = key($opt->option);
+$val = current($opt->option);

. ?

, 150+. , Rector, (+ , ).

php72.

:

composer require rector/rector --dev
vendor/bin/rector process src --level php72

, .


- , . , .

+2

, , " php", auto_prepend_file php.ini.

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

function_exists.

<?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;
    }
}

, php-. , .

, , ! , , , , php.

, , , .

Wee Zel

+1
source

For those using Magento2 or similar, it is not recommended to change the main files. Perhaps you can downgrade PHP from 7.2 to 7.1 until magento releases a new version.

sudo add-apt-repository ppa:ondrej/php

sudo apt-get update

sudo apt-get install php7.1

sudo apt-get install php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-mysql php7.1-mbstring php7.1-mcrypt php7.1-zip php7.1-fpm

sudo a2dismod php7.2

sudo a2enmod php7.1

sudo service apache2 restart

And install the active version of PHP

sudo update-alternatives --set php /usr/bin/php7.1
0
source

How about using this feature?

function array_fetch(array $a) {
   $element = current($a);
   next($a);
   return $element;
}
0
source

All Articles