Wrong content upon accesing/using an assoziative array in PHP
<p>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:</p>

<pre><code>$php -v
PHP 7.0.7 (cli) (built: May 25 2016 18:40:26) ( NTS )
</code></pre>

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

<pre><code> 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]]) &lt; ($z[2] - 1)) {
                array_push($datenGraph[$z[0]], 0);
            } 
            array_push($datenGraph[$z[0]], (int)$z[3]);
        }
    foreach ($datenGraph as $key =&gt; &amp;$value) {
        while (count($value) &lt; 12) {
            array_push($value, 0);
        }
    }
</code></pre>

<p>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): </p>

<pre><code>array (size=4)
  'Numerber1' =&gt; 
    array (size=12)
   0 =&gt; int 0     1 =&gt; int 0      2 =&gt; int 0      3 =&gt; int 0
   4 =&gt; int 2     5 =&gt; int 16     6 =&gt; int 0      7 =&gt; int 0      
   8 =&gt; int 0     9 =&gt; int 0      10 =&gt; int 0     11 =&gt; int 0
  'Number2' =&gt; 
    array (size=12)
   0 =&gt; int 0     1 =&gt; int 0     2 =&gt; int 0      3 =&gt; int 0
   4 =&gt; int 2     5 =&gt; int 7     6 =&gt; int 0      7 =&gt; int 0    
   8 =&gt; int 0     9 =&gt; int 0     10 =&gt; int 0     11 =&gt; int 0 
  'Number3' =&gt; 
   array (size=12)
  0 =&gt; int 0      1 =&gt; int 0     2 =&gt; int 0      3 =&gt; int 0
  4 =&gt; int 0      5 =&gt; int 8     6 =&gt; int 0      7 =&gt; int 0
  8 =&gt; int 0      9 =&gt; int 0     10 =&gt; int 0     11 =&gt; int 0
  'Number4' =&gt; 
    array (size=12)
  0 =&gt; int 0      1 =&gt; int 0     2 =&gt; int 0      3 =&gt; int 0  
  4 =&gt; int 0      5 =&gt; int 7     6 =&gt; int 0      7 =&gt; int 0 
  8 =&gt; int 0      9 =&gt; int 0     10 =&gt; int 0     11 =&gt; int 0
</code></pre>

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

<p>Here is how it's look like:</p>

<pre><code>foreach ($datenGraph as $key =&gt; $value) {
                print_r($value);
            }
</code></pre>

<p>prints identical last 2 tuples:</p>

<pre><code>   Array ( [0] =&gt; 0 [1] =&gt; 0 [2] =&gt; 0 [3] =&gt; 0 [4] =&gt; 2 [5] =&gt; 16 [6] =&gt; 0 [7] =&gt; 0 [8] =&gt; 0 [9] =&gt; 0 [10] =&gt; 0 [11] =&gt; 0 )
   Array ( [0] =&gt; 0 [1] =&gt; 0 [2] =&gt; 0 [3] =&gt; 0 [4] =&gt; 2 [5] =&gt; 7 [6] =&gt; 0 [7] =&gt; 0 [8] =&gt; 0 [9] =&gt; 0 [10] =&gt; 0 [11] =&gt; 0 )
   Array ( [0] =&gt; 0 [1] =&gt; 0 [2] =&gt; 0 [3] =&gt; 0 [4] =&gt; 0 [5] =&gt; 8 [6] =&gt; 0 [7] =&gt; 0 [8] =&gt; 0 [9] =&gt; 0 [10] =&gt; 0 [11] =&gt; 0 ) 
   Array ( [0] =&gt; 0 [1] =&gt; 0 [2] =&gt; 0 [3] =&gt; 0 [4] =&gt; 0 [5] =&gt; 8 [6] =&gt; 0 [7] =&gt; 0 [8] =&gt; 0 [9] =&gt; 0 [10] =&gt; 0 [11] =&gt; 0 ) 
</code></pre>

<p>Next one prints the same wrong as above.:</p>

<pre><code>$valueSet2 = array_values($datenGraph);
foreach ($valueSet2 as $key =&gt; $value) {
            print_r($value);
        }
</code></pre>

<p><strong>But</strong> this prints the right content.</p>

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

<p>Gives: <code>[[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]</code></p>

<p>Still right results(double json_encode) from this(getting string):</p>

<pre><code>$valueSet2 = json_encode(array_values($datenGraph));
console_log($valueSet2);
</code></pre>

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

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

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