5

Possible Duplicate:
PHP Pass by reference in 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.

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

The current result of the following code is: item:5 Quantity:12 item:6 Quantity: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/>';
            }
?>
Community
  • 1
  • 1
ajodom10
  • 141
  • 1
  • 4
  • 12

3 Answers3

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 -

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.

Lix
  • 47,311
  • 12
  • 103
  • 131
  • @ajo - No problem! Happy coding! Thank you voters - couldn't have reached 8K with out **you**! – Lix Jul 22 '12 at 23:11
0

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

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

elo
  • 615
  • 4
  • 11
-2

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

$items[$testitem] += $testvalue;

This works for your example.

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

Ali
  • 21,572
  • 15
  • 83
  • 95
  • 1
    -1 for a poor answer, To be honest I didn't even read the code to see if it was correct, but if you expect anyone to actually take notice you should explain your answer. – Jon Taylor Jul 22 '12 at 23:13
  • This is an obvious answer and I don't know why they are trying to do it hard way! And I don't care about your -1 – Ali Jul 22 '12 at 23:14
  • 1
    It's only obvious to those who understand it. To those who are asking the question it probably isn't so obvious. At least explain why you are doing it this way if you want others to actually take your answers seriously. – Jon Taylor Jul 22 '12 at 23:16
  • 2
    Ali, no one is saying it is, but an explanation wouldn't take you more than 30 seconds to write, especially as you understand it so well. How on earth do you expect others to learn if your attitude towards them is "I'm not going to explain because you should understand already", please tell me you aren't a teacher? – Jon Taylor Jul 22 '12 at 23:19
  • Ok Ok, I add an explanation! Not now, maybe someday! – Ali Jul 22 '12 at 23:20
  • Anyway i've tried to be helpful, I don't understand why you make people regretful about it by your -1s – Ali Jul 22 '12 at 23:29
  • Ali I gave you a -1 because your answer at the time was worthy of deletion, it was of no help what so ever, however now that you have changed it I have retracted my -1. I am not discouraging people from leaving answers, I discourage people from leaving poor/wrong answers. In your case there is no argument, your answer was poor. – Jon Taylor Jul 23 '12 at 08:40