0

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?

1
  • You can't. Only workaround would be using AJAX or something. Commented Mar 26, 2015 at 14:16

1 Answer 1

2
<?php
$book = array(
    "title" => "JavaScript: The Definitive Guide",
    "author" => "David Flanagan",
    "edition" => 6
);
?>
<script type="text/javascript">
var book = <?php echo json_encode($book, JSON_PRETTY_PRINT) ?>;
/* var book = {
    "title": "JavaScript: The Definitive Guide",
    "author": "David Flanagan",
    "edition": 6
}; */
alert(book.title);
</script>

try it hope this will help...

Sign up to request clarification or add additional context in comments.

3 Comments

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
<?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 ; ?>
sorry about how it looks, new to stackoverflow, don't really know how to use it

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.