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

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

 例子1:

php7.1写法

if ( is_array( $u ) ) {
            while( list( $key ) = each( $u ) ) {
                $u = $u[$key];
                break;
            }
        }

改为php7.2写法

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中对我有用的例子。

(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 

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:我的例子:支付过程中生成签名时出现错误 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lxw1844912514

你的打赏就是对我最大的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值