yokogeri yokogeri - 3 years ago 547
PHP Question

How to resolve this deprecated function each php

With php 7.2, each will be deprecated, how to change these instructions to non deprecated ones?


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


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


for(reset($broken);$kv = each($broken);) {...}


list(, $this->result) = each($this->cache_data);

Thank you.

Answer Source

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

  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 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.

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];

For the last 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download