【发布时间】:2012-07-22 23:05:39
【问题描述】:
为什么数组中的两个项目的值都会改变?我只是想更改等于 $testitem 的键的值。
以下代码的期望结果: 项目:5 数量:12 项目:6 数量:2
以下代码的当前结果是: 项目:5 数量:12 项目:6 数量:12
<?php
$items = array(
'5' => '4',
'6' => '2',
);
$testitem = '5';
$testvalue = '8';
foreach($items as $key => &$value)
{
if ($key == $testitem)
{
$value = $value + $testvalue;
}
}
foreach($items as $key => $value)
{
print 'item:'.$key.' Quantity:'.$value.'<br/>';
}
?>
【问题讨论】: