PHP - Why does the value change for both items in array? [duplicate]

时间:2022-06-06 21:30:49

Possible Duplicate:
PHP Pass by reference in foreach

可能重复:PHP在foreach中通过引用传递

Why does the value change for both items in array? I'm just trying to change the value of the key that is equal to $testitem.

为什么数组中的两个项的值都会发生变化?我只是想改变等于$ testitem的键的值。

Desired result of following code: item:5 Quantity:12 item:6 Quantity:2

以下代码的理想结果:项目:5数量:12项目:6数量:2

The current result of the following code is: item:5 Quantity:12 item:6 Quantity:12

以下代码的当前结果是:item: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/>';
            }
?>

3 个解决方案

#1


8  

The problem comes when you attempted to pass the $value variable as a reference. You will be able to achieve your desired result by modifying your foreach loop to look like this -

当您尝试将$ value变量作为引用传递时出现问题。通过将foreach循环修改为如下所示,您将能够实现所需的结果 -

foreach($items as $key => $value){   
  if ($key == $testitem){
    $items[$key] = $value + $testvalue;   
  }
}

Simply use the current $key or the value of $testitem for that matter, as a reference to your $items array - and modify the contents like that.

只需使用当前的$ key或$ testitem的值作为$ items数组的引用 - 并修改这样的内容。

#2


0  

Becouse Reference of a $value and the last array element remain even after the foreach loop.

Becouse引用$ value,最后一个数组元素仍然在foreach循环之后。

Use unset($value),after your first foreach and your code will work fine.

使用未设置($ value),在您的第一次foreach之后,您的代码将正常工作。

#3


-2  

Why don't you just use this code instead of a loop:

为什么不使用此代码而不是循环:

$items[$testitem] += $testvalue;

$ items [$ testitem] + = $ testvalue;

This works for your example.

这适用于您的示例。

In php you can reference array element with variable. So it exactly do what you want.

在php中,您可以使用变量引用数组元素。所以它完全按照你的意愿行事。

#1


8  

The problem comes when you attempted to pass the $value variable as a reference. You will be able to achieve your desired result by modifying your foreach loop to look like this -

当您尝试将$ value变量作为引用传递时出现问题。通过将foreach循环修改为如下所示,您将能够实现所需的结果 -

foreach($items as $key => $value){   
  if ($key == $testitem){
    $items[$key] = $value + $testvalue;   
  }
}

Simply use the current $key or the value of $testitem for that matter, as a reference to your $items array - and modify the contents like that.

只需使用当前的$ key或$ testitem的值作为$ items数组的引用 - 并修改这样的内容。

#2


0  

Becouse Reference of a $value and the last array element remain even after the foreach loop.

Becouse引用$ value,最后一个数组元素仍然在foreach循环之后。

Use unset($value),after your first foreach and your code will work fine.

使用未设置($ value),在您的第一次foreach之后,您的代码将正常工作。

#3


-2  

Why don't you just use this code instead of a loop:

为什么不使用此代码而不是循环:

$items[$testitem] += $testvalue;

$ items [$ testitem] + = $ testvalue;

This works for your example.

这适用于您的示例。

In php you can reference array element with variable. So it exactly do what you want.

在php中,您可以使用变量引用数组元素。所以它完全按照你的意愿行事。