php7.2: the each() function is deprecated

The each() function is deprecated under php7.2. If we run the following code we will get errors:

<?php
$arr=array(
‘glasses1’ => ‘reading glasses’,
‘glasses2’ => ‘bifocal glasses’,
‘glasses3’ => ‘progressive glasses’
);

while(list($key,$val)=each($arr)){
echo $key.’—>’.$val.'<br>’;
}

?>

 

Deprecated: The each() function is deprecated. This message will be suppressed on further calls in E:\xampp\htdocs\index.php on line 8
glasses1—>reading glasses
glasses2—>bifocal glasses
glasses3—>progressive glasses

We should use foreach or use key/current/next:

<?php
$arr=array(
‘glasses1’ => ‘reading glasses’,
‘glasses2’ => ‘bifocal glasses’,
‘glasses3’ => ‘progressive glasses’
);

// this cycle echoes all associative array
while (current($arr)) {

// echo key($arr).'<br />’;
echo current($arr).'<br />’;

next($arr);
}

?>

 

reading glasses
bifocal glasses
progressive glasses