Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Pass json object from php to javascript

I have a json array in php and I want to pass it to javascript so I can use it with google charts.

If I do this:

var a = <?php echo((json_encode($data))); ?>;

I get the data in the format Name,Value,PHP,78,JAVA,1000,HTML,129 but I want to keep it in the json format that it was in

[["Name","Value"],["PHP",78],["JAVA",1000],["HTML",129]]​

because google charts needs to receive it like this. Any idea how to do this?

Answer*

Draft saved
Draft discarded
Cancel
3
  • no error just still getting it in the wrong format when I test. I think because I am changing it to a javascript variable but I just want to keep it as a json array Commented Mar 26, 2015 at 14:24
  • <?php $data = Array (); $data [] = Array ("Name", "Value"); $data [] = Array ("PHP", 78); $data [] = Array ("JAVA", 1000); $data [] = Array ("HTML", 129); $table = json_encode($data); echo $table ; ?> Commented Mar 26, 2015 at 14:30
  • sorry about how it looks, new to stackoverflow, don't really know how to use it Commented Mar 26, 2015 at 14:30

default

AltStyle によって変換されたページ (->オリジナル) /