How can I update code that uses the deprecated each() function?
Warning This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged
Deprecated
Code:$ar = $o->me;
reset($ar);
list($typ, $val) = each($ar);
|
Replace with
Code:$ar = $o->me; // reset isn't necessary, since you just created the array
$typ = key($ar);
$val = current($ar);
|
Deprecated
Code:$out = array('me' => array(), 'mytype' => 2, '_php_class' => null);
$expected = each($out);
|
Replace with
Code:$out = array('me' => array(), 'mytype' => 2, '_php_class' => null);
$expected = [key($out), current($out)];
|
Deprecated
Code:for(reset($broken);$kv = each($broken);) {...}
|
Replace with
Code:foreach ($broken as $k => $v) {
$kv = [$k, $v];
}
|
Deprecated
Code:list(, $this->result) = each($this->cache_data);
|
Replace with
Code:$this->result = current($this->cache_data);
|
Deprecated
Code:// 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++;
}
|
Replace with
Code:reset($array);
for ($i = 0; $i < 30; $i++) {
$id = key($array);
$item = current($array);
// code
next($array);
}
|