1

I've a php query which creates the JSON data of the the data received from the SQL query that is run. The JSON data is created using this query using this query

echo json_encode($res->fetch_all(MYSQLI_ASSOC), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT );

Now I want to plot a chart using fusionchart for which I need to convert this data into an array. I tried a sample JS code to convert the JSON data into an JS array and it worked

var data = { "timestamp": "2016-09-23", "value1": "0", "value2": "0", ........., "value49": "0", "value50": "0" };
   var arr = [];
   for (elem in data) {
        arr.push(data[elem]);
    }
    console.log(arr);

Now in the data variable I need to pass the data from php code. This is just one of the records that I entered manually. There are over a million records and I need to convert all of them. How I do this?

13
  • 2
    'There are over a million records' - not sure if that's going to work well anyway. Commented Mar 14, 2018 at 10:15
  • Already tried JSON_Decode. It did not work. Commented Mar 14, 2018 at 10:15
  • @nigelren Still I need to try to do it. Its a mandatory requirement. Commented Mar 14, 2018 at 10:16
  • Do you want to plot all the records at once or is there some sort of constraint? Commented Mar 14, 2018 at 10:17
  • 1
    Regarding "I'm unable to pass the $res variable directly to the data variable", of course not. $res is a result set, not an array. You need to store the result in a variable. Instead of echo json_encode($res->...), do $data = json_encode($res->..). Then you can echo the $data variable where ever you want. Commented Mar 14, 2018 at 10:28

2 Answers 2

1

You can use parseJSON() function :

var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" );
Sign up to request clarification or add additional context in comments.

1 Comment

The problem that I'm facing is not that I cannot convert it into JS array, I'm unable to pass the $res variable directly to the data variable, so that it can convert the entire dataset into JS array at once.
1

You can just use Object.values() function to acquire values from an object.

var data = { "timestamp": "2016-09-23", "value1": "0", "value2": "0", "value49": "0", "value50": "0" };

var arr = Object.values(data);
console.log(arr);

For PHP

You can send only values and omit keys in PHP using following code. json_decode converts JSON string to an array. implode converts an array to a string.

<?php

$str = '{ "timestamp": "2016-09-23", "value1": "val1", 
        "value2": "val2", "value49": "val49", "value50": "val50" }';
$myJSON = json_decode($str, true);

echo '[' . implode(",", $myJSON) . ']';

Results:

[2016-09-23,val1,val2,val49,val50]

2 Comments

The problem that I'm facing is not that I cannot convert it into JS array, I'm unable to pass the $res variable directly to the data variable, so that it can convert the entire dataset into JS array at once.
@Will I changed my solution to answer back-end requirement.

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.