Skip to content


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

  1. $testarr = array('a','b','c');
  2.  
  3. foreach( $testarr as $k=>&$v ) {
  4.     echo $k."=>".$v."<br>";
  5. }
  6. echo "-------<br>";
  7. foreach( $testarr as $k=>$v ) {
  8.     echo $k."=>".$v."<br>";
  9. }

你可能以为结果为

  1. 0=>a
  2. 1=>b
  3. 2=>c
  4. -------
  5. 0=>a
  6. 1=>b
  7. 2=>c

实际输出是

  1. 0=>a
  2. 1=>b
  3. 2=>c
  4. -------
  5. 0=>a
  6. 1=>b
  7. 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.