0

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.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
Orionis
  • 983
  • 7
  • 11
  • 3
    Build a normal PHP array (multidimensional), then apply `json_encode` to the result. Replace `$data[] = "[".$dat.",".$pag."]";` with `$data[] = [$dat, $pag];` for the beginning. – Ruslan Osmanov Dec 16 '16 at 10:48
  • 1
    Do not use mysql_* functions. Please have a look at: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php for more informations – rap-2-h Dec 16 '16 at 10:51
  • What you are looking for exactly? – rahul Dec 16 '16 at 10:51
  • There's no way that `JSON.parse(response)`, which you apparently tried, could return a string. If the response was ill-formed, it should have thrown an exception. Could you put it back and tell us exactly what you get? – Aioros Dec 16 '16 at 13:49

1 Answers1

0

I did it ! Pls don't ask me how since I'm possibly even more confused than when I started.
I wish to post this message to share the solution for other beginner like myself.

First step was to follow Ruslan Osmanov suggestion:

Replace $data[] = "[".$dat.",".$pag."]"; with $data[] = [$dat, $pag];  

Very simple and very effective.
After few attempts (i.e. trying to build a multidimensional array), at the end I just put an echo json_encode($data); and most of the problems were gone.
At the client side, with a simple donwloaded = JSON.parse(response); I finally received what even Mozilla recognizes as an array of arrays, and Highcharts too.

There was still a problem anyway: the values into the arrays were 'stringified' inside double quote, possibly due to the encoding, and Highcarts did not visualized the values.
I changed in PHP the $data with $data[] = [(int)$dat,(int)$pag];
casting the values to number, and now I can finally visualize the chart...great.
Again,I'm almost sure there are more efficient and elegant methods, but it does what I needed and I'm happy. `

Orionis
  • 983
  • 7
  • 11