【问题标题】:php 7.2 each() function is deprecated [duplicate]php 7.2 each() 函数已弃用[重复]
【发布时间】:2018-12-16 12:35:39
【问题描述】:
       if ( is_array( $u ) ) {
            while( list( $key ) = each( $u ) ) {
                $u = $u[$key];
                break;
            }
        }

我的 php 版本是 7.2 当我在 laravel 框架上运行它时,我遇到了这个错误

The each() function is deprecated. This message will be suppressed on further calls

我发现那是我必须将每个更改为 foreach enter link description here

谁能把代码改成我在 php 7.2 上工作,谢谢

【问题讨论】:

    标签: php each


    【解决方案1】:
    if (is_array($u)) {
        foreach ($u as $k => $v) {
            $u = $u[$k]; // or $v
            break;
        }
    }
    

    $u 将始终是数组的第一个值,所以我看不出您需要它的位置。你可以简单地通过$u = $u[0];得到数组的第一个值

    【讨论】:

      【解决方案2】:
              while( list( $key ) = each( $u ) ) {
                  $u = $u[$key];
                  break;
              }
      

      绝对没有理由在这里做一个循环。您只是从数组中获取第一个值并覆盖数组。上面的循环可以使用 current() 在一行中重写,这会将当前值(如果数组的指针没有改变,则为第一个值)拉出数组:

      $u = current($u);
      

      【讨论】:

        【解决方案3】:

        正如 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 ) {...}
        

        请阅读:

        【讨论】:

          猜你喜欢
          • 2020-04-20
          • 1970-01-01
          • 1970-01-01
          • 2019-04-03
          • 2020-05-01
          相关资源
          最近更新 更多