1

I've been pulling my hair out trying to figure this out. I'm using ZingChart to plot some data from a MySQL query. I put the data into a PHP array, and then use:

var myData = <?php echo json_encode($combined); ?>;

to put it into a javascript array. If I do:

document.write(myData[0]);

then it shows the correct value for that index. When I try to use the array with the ZingChart's JSON, I see that it puts quotes around all the data, which for some reason it doesn't like. If I manually remove the quotes using notepad, the data displays great, so I know it's just a matter of getting rid of these quotes somehow.

Here's how it looks, for example, when I view the source from the page:

var myData = [["1466766034467","71.191"],["1466766094482,71.1986"]];

I've tried many ways and spent many hours trying to get the data passed into JSON without the quotes, but I know just enough to be dangerous, so hopefully someone can guide me.

document.write(myData[1]); 

will result: 1466766094482,71.1986

Thanks in advance.

3
  • What is the output of var_dump($combined)? The problem can likely be fixed by how you insert values into that variable. Somewhere the types are being set to a string. Commented Jun 26, 2016 at 22:24
  • have you tried mydata[0].toString? Commented Jun 26, 2016 at 22:24
  • json_encode($combined, JSON_NUMERIC_CHECK); Commented Jun 26, 2016 at 22:25

2 Answers 2

1

Assuming you are running a reasonably current version of php you can add JSON_NUMERIC_CHECK to json_encode() options argument

json_encode($combined, JSON_NUMERIC_CHECK);

See json_encode docs

Or in javascript iterate arrays and cast values to number using any variety of methods

myData.forEach(function(arr){
    arr[0] = +arr[0];
    arr[1] = +arr[1];
})
Sign up to request clarification or add additional context in comments.

8 Comments

Note a nested forEach function may be needed to convert an array of arrays
@charlietfl your use of + to convert to numeric values is nice here since it applies to Integers and Floats equally.
@Traktor53 OP using 2D chart data so seems like overkill here
Great, the numeric check did it.
So it worked, but the ZingChart JSON isn't liking the variable for the series data because it shows up as null. I can copy the var's contents directly from the source off the page, and use that data and the chart shows up just fine.
|
0

You can parse the String types into Int and Float. For your 2-item arrays, the following works with the map function:

myData.map(function(x){ return [parseInt(x[0]),parseFloat(x[1]) });

// or in ES6 notation
myData.map( x => [parseInt(x[0]),parseFloat(x[1])] );

General Solution

Inspired by @charlietfl's solution, here's a generic function parseNumeric for converting all numeric data within an array. It will recurse through any nested arrays it finds.

var myData = [["1466766034467","71.191"],["1466766094482","71.1986"]];

// Convert data to numeric, including recursion within nested arrays
// Usage parseNumeric( myData );
function parseNumeric(x,y,z) {
  if( Object.prototype.toString.call( x ) === '[object Array]' ) {
    x.forEach(function(c,i,a){numConvert(c,i,a)});
  } else if ( typeof z != 'undefined' ) {
    z[y] = +x
  }
}

parseNumeric( myData );

// => [[1466766034467,71.191],[1466766094482,71.1986]];

2 Comments

I tried both of these directly after the myData declaration, and I still get the quotes around data (timestamp and value, respectively).
The snippets above don't mutate the source array, so you'd want to store the output in another var, send it somewhere, or overwrite myData: myData = myData.map(...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.