I know this question was answered many times, but I'm still in trouble, due to my confusion on objects, arrays, strings and JSON.
Trying to implement a chart by using Highcharts I made a PHP which extracts data from a DB. Simplifying, let's say that these are just some values pairs (i.e. day,visits).
I want to build an Array of arrays to send to JS.
I made it in this way:
while ($row = mysql_fetch_array($result)) {
$dat = substr($row['data'],8,2); //strip to day
$pag = $row['pagina'];
$data[] = "[".$dat.",".$pag."]";
}
$finalData = join($data, ',');
//echo json_encode($finalData); // I get [09,20],[12,15],[12,11],[12,18]
echo "[".$finalData."]"; //I get [[09,20],[12,15],[12,11],[12,18]]
As you see, I tried two different methods (and some variations with/without json_encode).
On client side, I declared: var donwloaded = [[]];
(array of arrays) which is filled in this way:
....
var response = xmlhttp.responseText;
// donwloaded = JSON.parse(response);
donwloaded = response;
Again I tried in two ways.
With all the possible combination, at the end 'donwloaded' results as a string (according to Mozilla debugger) and the chart is not filled.
The intriguing part is that if in JS I declare this:
var testValues = [ [1,10],[2,20],[3,30],[4,40] ];
which appears similar to what I get from PHP, Mozilla says that this an array of 4 arrays, and Highchart displays it correctly.
I'm sure I did few errors, but after 2 days spent in testing, I'm even more confused.