Home  >  Article  >  Backend Development  >  怎么用循环方式改变数组里的值?

怎么用循环方式改变数组里的值?

WBOY
WBOYOriginal
2016-06-20 12:33:271745browse

$a=array(1,2,3,4,5);
var_dump($a);
foreach($a as $b){
$b=$b-1;
}

var_dump($a);

这样前后两次打印出来的 $a 是一样的都是 (1,2,3,4,5),怎么让第二次 $a=(0,1,2,3,4)


回复讨论(解决方案)

$a=array(1,2,3,4,5);
var_dump($a);
foreach($a as  &$b){
$b=$b-1;
}

var_dump($a);

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