Home  >  Article  >  Backend Development  >  foreach循环时动态往数组里添加数据,foreach数组_PHP教程

foreach循环时动态往数组里添加数据,foreach数组_PHP教程

WBOY
WBOYOriginal
2016-07-12 08:59:441409browse

foreach循环时动态往数组里添加数据,foreach数组

  foreach循环时动态往数组里添加数据,有一次做项目中,foreach的时候需要动态往数组里添加数据(我们这里随便举个例子)

结果:

foreach循环时动态往数组里添加数据,foreach数组_PHP教程foreach($arr as &$v){···}的时候,相等于$v=&$arr[$k]
$arr[$k]和$v同时指向$arr[$k]的内存地址,即便是unset($v),仅仅是删除了$v对内存空间的引用,并没有删除$arr[$k]对内存地址的引用,所以$arr[$k]依然健在,$arr自然也就没变化,所以应该这样

结果:

还有一点需要注意:&$k什么结果

结果:

意思是:键不能被引用,压根就没有这种语法格式

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1096600.htmlTechArticleforeach循环时动态往数组里添加数据,foreach数组 foreach循环时动态往数组里添加数据,有一次做项目中,foreach的时候需要动态往数组里添加数...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn