laravel php 7.2 each() 函数已弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51235163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:53:18  来源:igfitidea点击:

php 7.2 each() function is deprecated

phplaraveleach

提问by Awar Pulldozer

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

and my php version is 7.2 when i run it on laravel framwork i gat this error

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

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

i found thats i have to change each to foreach enter link description here

我发现那是我必须将每个更改为 foreach 在此处输入链接描述

cound any one change the code to me to work on php 7.2 thanks

任何人都将代码更改给我以在 php 7.2 上工作,谢谢

回答by Devon

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

There's absolutely no reason to do a loop here. You're just getting the first value out of the array and overwriting the array. The above loop can be rewritten in one line using current() which will pull the current value (first value if the array's pointer hasn't been altered) out of the array:

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

$u = current($u);

回答by Joanmacat

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()function 作为已弃用的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 ) {...}

Please read:

请阅读:

回答by Ramon Bakker

if (is_array($u)) {
    foreach ($u as $k => $v) {
        $u = $u[$k]; // or $v
        break;
    }
}

But $uwill be always the first value of the array, so i dont see where you need it for. You can get the first value of the array simply by doing $u = $u[0];

$u将始终是数组的第一个值,所以我看不到您需要它的位置。您只需执行以下操作即可获得数组的第一个值$u = $u[0];