Vickie
Vickie

Reputation: 99

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?

Upvotes: 0

Views: 237

Answers (1)

Vivek Singh
Vivek Singh

Reputation: 2447

<?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...

Upvotes: 2

Related Questions