简体   繁体   中英

Invalid argument foreach on an Array?

I'm completely lost, I have this code:

    $val = explode('?',$_POST['data']);
    print_r($val);
    foreach($val as &$v) {
       //some code
    }

and the result is this:

Array ( [0] => 1|1|41|1|1|1 [1] => 1|1|31|1|1|1 [2] => 1|1|21|1|1|3 ) 
Warning: Invalid argument supplied for foreach() in test.php on line 131

I really don't get what I'm doing wrong...

PS actually I've added print_r only to see what was wrong, I don't really need it

Your code is changing the $val somewhere in the foreach.

This code runs fine, see code path :

$_POST['data'] = '1|1|41|1|1|1?1|1|31|1|1|1?1|1|21|1|1|3';

$val = explode( '?', $_POST['data'] );
print_r( $val );
foreach ( $val as &$v ) {
  echo $v;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM