last day (14 days later) » 

6:41 PM
0
Q: How to generate JSON format in PHP?

DevI tried to generate JSON in php that to get format like as: var rows = [ ['Jan', 53], ['Feb', 71], ]; data.addRows(rows); I tried: $arr = array("Jan" => 53, "Feb" => 71); echo json_encode($arr); I tried in PHP: foreach($items as $item){ $values[] = "['1', 3]"; ...

 
Dev
I tried all samples, for me does not work
 
Perhaps if you would elaborate on "for me does not work." What are you expecting that didn't work.
 
Dev
Okay, how can I generate this in JS?
 
I think you will find an answer here
 
Dev
I think in JS it is array with array inside, right?
I tried you recommending answer, I get error: jsapi_compiled_default_module.js:37 Uncaught Error: Argument given to addRows must be either a number or an array
 
6:41 PM
You need to show the code you tried which gives the error you are getting. If it is too long for comment, edit your post.
 
Dev
Okay, I updated question
 
Not all of your relevent code is there - e.g. datajson is not set in the code you posted and "['1', 3]" seems to have nothing to do with the previous example array: var rows = [
['Jan', 53],
['Feb', 71],
];
 
Dev
7:23 PM
I did another samplw
seems it is right way
$this->results["month"][] = array(
"Jan" => 3
);
But in console I get object insread array
0:Object
Why?
 
7:48 PM
in javascript arrays are objects. When encoding an associative array for json, and then decoding it again in php using json_decode, it will be an object unless you specify false as a second parameter to json_decode which forces it to return an associative array instead of object.
 

last day (14 days later) »