Skip to content


foreach 使用”引用”遍历数组时的问题


$testarr = array(‘a’,’b’,’c’);

foreach( $testarr as $k=>&$v ) {
echo $k.”=>”.$v.”
“;
}
echo “——-
“;
foreach( $testarr as $k=>$v ) {
echo $k.”=>”.$v.”
“;
}

你可能以为结果为

0=>a
1=>b
2=>c
——-
0=>a
1=>b
2=>c

实际输出是

0=>a
1=>b
2=>c
——-
0=>a
1=>b
2=>b

这是什么道理?

Posted in PHP, 技术.

Tagged with , , .


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. David Lou says

    嗯,看起来好像就是个bug,参见http://bugs.php.net/bug.php?id=29992



Some HTML is OK

or, reply to this post via trackback.