数组上的参数foreach无效?

时间:2012-10-14 15:10:29

标签: php arrays foreach

我完全迷失了,我有这段代码:

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

结果如下:

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

我真的不明白我做错了什么......

PS 实际上我添加print_r只是为了看错了,我真的不需要它

1 个答案:

答案 0 :(得分:0)

您的代码正在更改foreach中某处的$val

此代码运行正常,请参阅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;
}