0

Can someone clarify a few things. ? Does unset($val) go after this line ?

foreach ($arr1 as $key => &$val) {} 

Why is the output or $arr1 taking the value of 6 for key c ?
I have tried using unset( ) and destroy_arr1( ) but neither resolve this.
Maybe I did not use the above functions in the proper location.

$arr1 = array("a" => 1, "b" => 2, "c" => 3);
$arr2 = array("x" => 4, "y" => 5, "z" => 6);

foreach ($arr1 as $key => &$val) {}
foreach ($arr2 as $key => $val) {}

var_dump($arr1);
var_dump($arr2);
?>

The output is:

array(3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> &int(6) }  
array(3) { ["x"]=> int(4) ["y"]=> int(5) ["z"]=> int(6) }
shareyourpeace
  • 253
  • 1
  • 2
  • 12
  • 1
    Have a look at http://stackoverflow.com/questions/3307409/php-pass-by-reference-in-foreach. – medowlock May 06 '15 at 13:43
  • 1
    It's because you are passing $val by reference in your first loop ? – Answers_Seeker May 06 '15 at 13:43
  • You need to `unset($val)`, not your array. – Gabriel Moretti May 06 '15 at 13:47
  • @medowlock i looked at the link. thanks. but this does not provide code using unset( ) ! – shareyourpeace May 06 '15 at 14:20
  • @Rizier123 I did a search prior to posting. similar questions are posted but known of them have responses with 'green' checks or upvotes. I will modify my question and please remove your flag. thanks. – shareyourpeace May 06 '15 at 14:21
  • @shareyourpeace Yes, but it explains why this works as it does. Just add an `unset($val);` between the foreachs and you're good to go :) – medowlock May 06 '15 at 14:22
  • 1
    @GabrielMoretti thanks. I actually had to modify code as to avoid a duplicate question. So I asked where unset($val) needed to go. As I did this, at medowlock responded. thanks. – shareyourpeace May 06 '15 at 14:30
  • @shareyourpeace So now your question is where you need to use `unset($val);` to get rid of the reference ? – Rizier123 May 06 '15 at 14:30
  • @Rizier123 In a couple of cross posts - Two others told me where to include this code. I looked at your link 'Strange Behavior of foreach( ). helpful. But I believe that my question is a bit more basic. I am using 2 different arrays as a demonstration. Not rewriting the same array. I am rather new at this but I think it justifies an individual post. But you know better. – shareyourpeace May 06 '15 at 14:34
  • @GabrielMoretti how to I mark correct or upvote your response ? – shareyourpeace May 06 '15 at 14:36
  • @shareyourpeace That you use 2 different arrays here makes no difference, since you use the same variable in both foreach loops for the value and from the first foreach loop `$v` is still by reference to the last array element which gets assign each iteration, so if you get to the end of the second foreach loop the last value of the second array is assign by reference to the last array element of the first array – Rizier123 May 06 '15 at 14:36
  • @medowlock how to I mark correct or upvote your response ? – shareyourpeace May 06 '15 at 14:37
  • @Rizier123 thanks. your explanation helps. I know we aren't suppose to use this as a chat room. I and many others use stackoverflow as a 'learning tool' !! That is why i suggested it be a 'different flavor' of the question. I need more points - and your 'dup' is more a downgrade :( – shareyourpeace May 06 '15 at 14:38
  • @shareyourpeace this is the comments section. We cant answer your question because it's closed due to duplication. I think you need 15 rep to upvote things. Dont worry about it. Did you solve your problem? – Gabriel Moretti May 06 '15 at 14:46
  • @GabrielMoretti Yes. It did. Thanks. Can you see my other comments to the person who marked it as a dup ? Question for you: You say 'We cant answer your question because it's closed due to duplication.' But if you comment - you are answering my question - yes ? – shareyourpeace May 06 '15 at 14:57
  • @shareyourpeace In our point of view, yes, I am answering your question. But in the stackoverflow point of view, this is not an answer, this is a comment. That's what I meant by we cant answer your question. If your problem persists. There are two things you can do: `unset($val)` after the first `foreach` or use a `var` with different name on the second. – Gabriel Moretti May 06 '15 at 15:16
  • @GabrielMoretti That clears up something else about Stackover ! I have been more likely to use different variable names but wanted to understand unset( ). I am posting another question about array_key with nested arrays. Hope that you see it. Thanks – shareyourpeace May 06 '15 at 15:38

0 Answers0