1
       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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Awar Pulldozer
  • 1,071
  • 6
  • 23
  • 40

3 Answers3

12

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:

Joanmacat
  • 1,442
  • 1
  • 10
  • 13
5
        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);
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
4
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];

Ramon Bakker
  • 1,075
  • 11
  • 24