0

This code is causing unexpected array content change. What could be the reason of this:

<?php

$arr[] = array('a', 'b');
$arr[] = array('c', 'd');

print_r($arr);
foreach ($arr as &$processed_arr) {

}

foreach ($arr as $processed_arr) {

}

print_r($arr);

Output:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

)
Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => a
            [1] => b
        )

)
AgA
  • 2,078
  • 7
  • 33
  • 62
  • you should try to do step by step debugging with a watch expression on your array. – greg0ire Apr 10 '14 at 17:54
  • Please make your title describe the problem. "Need help" is not a useful question title. – Lightness Races in Orbit Apr 10 '14 at 18:01
  • you need to post code before that array in order for us to figure out anything - there are too little information as to what is being passed actively to that segment – azngunit81 Apr 10 '14 at 18:02
  • possible duplicate of [PHP Pass by reference in foreach](http://stackoverflow.com/questions/3307409/php-pass-by-reference-in-foreach) – AgA Apr 11 '14 at 04:28

1 Answers1

1

It can indeed be due to your loop before. foreach in php leaves the iteration variable in scope even after the loop (Awful, I know).

So code like this:

$loop = [1,2,3];

foreach ($loop as &$c) {}

$c = 4;

var_dump($loop);

Will result in a loop variable containing [1,2,4]

The rest of your code doesn't look like it could be the cause of this. Of course the implementation of status is free to do whatever but given the name it seems highly unlikely imo. :)

Next time it might help to post more of the context. It's good to try to trim down the code posted like you have done, but if the posted code no longer exposes the problem (which yours does not) it makes it much harder to guess what's wrong.

monocell
  • 1,411
  • 10
  • 14