首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >记laravel项目,本地环境PHP7.1,线上PHP版本7.2,报错each函数废弃问题

记laravel项目,本地环境PHP7.1,线上PHP版本7.2,报错each函数废弃问题

作者头像
全栈程序员站长
发布2022-07-18 17:12:56
发布2022-07-18 17:12:56
7120
举报

大家好,又见面了,我是全栈君。

the each() function is deprecated. this message will be suppressed on further calls laravel

例子1:

php7.1写法

代码语言:javascript
复制
if ( is_array( $u ) ) {
            while( list( $key ) = each( $u ) ) {
                $u = $u[$key];
                break;
            }
        }

改为php7.2写法

代码语言:javascript
复制
if ( is_array( $u ) ) {
          $u = current($u);
        }

As PHP7.2 says, I suggest to use foreach() function as a substitute of deprecated each(). Here I let a couple of examples that works to me in WordPress.—-正如PHP7.2所说,我建议使用foreach()函数来替代已弃用的each()。这里我举几个在Wordpress中对我有用的例子。

代码语言:javascript
复制
(OLD) while ( list( $branch, $sub_tree ) = each( $_tree ) ) {...}
(NEW) foreach ( (Array) $_tree as $branch => $sub_tree ) {...}


(OLD) while ( $activity = each( $this->init_activity ) ) {...}
(NEW) foreach ( $this->init_activity as $activity ) {...}

(old)while(list($file, $info) = each($this->images))
(new)foreach($this->images as $file => $info) {
    // ...
}

例子2

代码语言:javascript
复制
16548 while (list($id, $name) = each($attr_array[1])) {  //7.1

I replaced the line with the next code in both lines and it worked,替换为如下

foreach($attr_array[1] as $id => $name) {  //7.2

例子3:我的例子:支付过程中生成签名时出现错误

代码语言:javascript
复制
public function createLinkString($param)
    {
        $arg = "";
        //数组排序
        ksort($param);
        reset($param);
        //7.1写法
        /*while (list ($key, $val) = each($param)) {
            if ($key == "sign") continue;
            if (!empty($key)) {
                $arg .= $key . "=";
            }
            if (is_array($val)) {
                $arg .= $this->createLinkString($val) . "&";
            } else {
                $arg .= $val . "&";
            }
        }*/
        //7.2写法
        foreach ($param as $key => $val) {
            if ($key == "sign") continue;
            if (!empty($key)) {
                $arg .= $key . "=";
            }
            if (is_array($val)) {
                $arg .= $this->createLinkString($val) . "&";
            } else {
                $arg .= $val . "&";
            }
        }
        //去掉最后一个&字符
        $arg = substr($arg, 0, strlen($arg) - 1);
        return $arg;
    }

参考:php 7.2 each() function is deprecated

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/111677.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年2月1,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档