0

all the day long I'm trying to overcome a bug in one of my PHP scripts. This is a strange behavior that i can't explain. Please help me. First of all:

$php -v
PHP 7.0.7 (cli) (built: May 25 2016 18:40:26) ( NTS )

In my script I run a MySQL query, which is succeeding. Second i do this:

 while ($z = mysqli_fetch_array($req)) {
            if (!isset($datenGraph[$z[0]])) {
                $datenGraph[$z[0]] = [];
        } //just fill the array with zeros before and after the result
            while (count($datenGraph[$z[0]]) < ($z[2] - 1)) {
                array_push($datenGraph[$z[0]], 0);
            } 
            array_push($datenGraph[$z[0]], (int)$z[3]);
        }
    foreach ($datenGraph as $key => &$value) {
        while (count($value) < 12) {
            array_push($value, 0);
        }
    }

However the code above is not a problem. In there I create an array which is looks like this(Array key -> value which holds arrays):

array (size=4)
  'Numerber1' => 
    array (size=12)
   0 => int 0     1 => int 0      2 => int 0      3 => int 0
   4 => int 2     5 => int 16     6 => int 0      7 => int 0      
   8 => int 0     9 => int 0      10 => int 0     11 => int 0
  'Number2' => 
    array (size=12)
   0 => int 0     1 => int 0     2 => int 0      3 => int 0
   4 => int 2     5 => int 7     6 => int 0      7 => int 0    
   8 => int 0     9 => int 0     10 => int 0     11 => int 0 
  'Number3' => 
   array (size=12)
  0 => int 0      1 => int 0     2 => int 0      3 => int 0
  4 => int 0      5 => int 8     6 => int 0      7 => int 0
  8 => int 0      9 => int 0     10 => int 0     11 => int 0
  'Number4' => 
    array (size=12)
  0 => int 0      1 => int 0     2 => int 0      3 => int 0  
  4 => int 0      5 => int 7     6 => int 0      7 => int 0 
  8 => int 0      9 => int 0     10 => int 0     11 => int 0

Please note, that all tuples are different(the right data). Now comes the tricky part. Upon assessing this array the content of last tuple changes to the exactly previous one! No matter how many tuples are in the array.

Here is how it's look like:

foreach ($datenGraph as $key => $value) {
                print_r($value);
            }

prints identical last 2 tuples:

   Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 2 [5] => 16 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 )
   Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 2 [5] => 7 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 )
   Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 8 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 ) 
   Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 8 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 ) 

Next one prints the same wrong as above.:

$valueSet2 = array_values($datenGraph);
foreach ($valueSet2 as $key => $value) {
            print_r($value);
        }

But this prints the right content.

$valueSet2 = array_values($datenGraph);
function console_log( $data )
{
    echo '<script>';
    echo 'console.log(' . json_encode( $data ) . ')';
    echo '</script>';
}
console_log($valueSet2);

Gives: [[0,0,0,0,2,16,0,0,0,0,0,0],[0,0,0,0,2,7,0,0,0,0,0,0],[0,0,0,0,0,8,0,0,0,0,0,0],[0,0,0,0,0,7,0,0,0,0,0,0]

Still right results(double json_encode) from this(getting string):

$valueSet2 = json_encode(array_values($datenGraph));
console_log($valueSet2);

And even more confusing. Note the json_encode in previous 2 examples prints correct content. Now I'm trying to do this(just several lines below):

<?php
    $x=0;
        foreach ($datenGraph as $key => $value) {
            echo 'label: ' . '\'' . $key . '\'' . ' , ' . PHP_EOL;
            echo 'backgroundColor: ['  . 'randomColor() . ']' . ', ' . PHP_EOL;
            echo 'data: '  . json_encode($valueSet2[$x]) . PHP_EOL;
            if ($x < $keySetSize - 1){
                echo '}, {' . PHP_EOL;
                }
            $x=$x+1;
            }   
?>

And on the last iteration I get again the wrong tuple unlike the previous 2 examples using json_encode were giving desired result. What I'm doing wrong? Please help. Thanks!

0xbadc0de
  • 3,805
  • 3
  • 22
  • 14
  • I forgot to mention that my colleague experience the same issue on PHP version 5. – 0xbadc0de Jun 15 '16 at 21:57
  • 3
    Probably because of `&$value`, try using a different variable after that loop. – Jonnix Jun 15 '16 at 22:12
  • 1
    I was coming on to the same. You need a reference to complete your 4 arrays to 12 values, ok. But after this loop, you don't unset this reference. It's generally not good to let references behind... Then you use this reference again later... maybe you modify the last referenced item somwhere, where you think you're working on a new variable... – fpierrat Jun 15 '16 at 22:15
  • 1
    Related: http://stackoverflow.com/questions/37493002/php-array-content-not-working-as-it-ought/37493198#37493198 They both show weirdness when pushing references onto an array. – Barmar Jun 15 '16 at 22:29
  • 1
    i think that you're encountering the same problem of this question: http://stackoverflow.com/questions/3307409/php-pass-by-reference-in-foreach – Bellu Jun 15 '16 at 22:30
  • 1
    The [foreach documentation](http://php.net/manual/en/control-structures.foreach.php) does specifically warn about unsetting references afterward. In fact, the example in the warning there is pretty similar to this. – Don't Panic Jun 15 '16 at 22:34
  • 1
    Your post is a bit too long but your issue is probably because your ```$value``` variable is a reference to the last entry inside your ```$datenGraph``` array. The next time you reuse your $value variable in a later loop for whatever array you are working with, you are unknowingly setting that last $datenGraph value during each iteration, due to $value being a reference to it. You can see this easily if you inspect your variables during runtime. – georaldc Jun 15 '16 at 22:48
  • oh, indeed. $value was still bound... Didn't thought that a variable can be bound if out of loop. Thanks! – 0xbadc0de Jun 15 '16 at 23:17

0 Answers0