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!