Updating Code to Avoid Using the Deprecated each() Function
In PHP 7.2, the each() function has been deprecated. To avoid using it and continue using your code without encountering deprecation warnings, there are several techniques you can employ:
For assigning values from a key-value pair:
Example:
$ar = $o->me; $typ = key($ar); $val = current($ar);
Returning the first key-value pair:
Example:
$out = array('me' => array(), 'mytype' => 2, '_php_class' => null); $expected = [key($out), current($out)];
Iterating through an array:
Example:
foreach ($broken as $k => $v) { $kv = [$k, $v]; }
Assigning a single value:
Example:
$this->result = current($this->cache_data);
Iterating to a specific point:
Example:
reset($array); for ($i = 0; $i < 30; $i++) { $id = key($array); $item = current($array); next($array); }
By incorporating these techniques into your code, you can eliminate the use of the deprecated each() function and ensure compatibility with PHP 7.2 and beyond.
The above is the detailed content of How Can I Replace the Deprecated PHP `each()` Function?. For more information, please follow other related articles on the PHP Chinese website!