How can I update code that uses the deprecated each() function?(如何更新使用已弃用 each() 函数的代码?)
问题描述
With PHP 7.2, each
is deprecated. The documentation says:
Warning This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.
How can I update my code to avoid using it? Here are some examples:
-
$ar = $o->me; reset($ar); 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);
-
// 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++; }
When I execute the code on PHP 7.2 I receive the following error:
Deprecated: The each() function is deprecated. This message will be suppressed on further calls
For your first two example cases, you could use
key()
andcurrent()
to assign the values you need.$ar = $o->me; // reset isn't necessary, since you just created the array $typ = key($ar); $val = current($ar);
-
$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 fourth 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
.Fifth can be replaced with a
for()
loop.reset($array); for ($i = 0; $i < 30; $i++) { $id = key($array); $item = current($array); // code next($array); }
这篇关于如何更新使用已弃用 each() 函数的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更新使用已弃用 each() 函数的代码?
基础教程推荐
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01