问题
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
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
cound any one change the code to me to work on php 7.2 thanks
回答1:
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:
$u = current($u);
回答2:
if (is_array($u)) {
foreach ($u as $k => $v) {
$u = $u[$k]; // or $v
break;
}
}
But $u
will 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];
回答3:
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.
(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:
- https://www.drupal.org/project/drupal/issues/2885309
- https://wiki.php.net/rfc/deprecations_php_7_2#each
来源:https://stackoverflow.com/questions/51235163/php-7-2-each-function-is-deprecated