-1

I have a JSON object array that is created in PHP that I need to be able to send down to javascript so that javascript can sort through the JSON using JSON.parse

What is the best way to go about doing this?

0

2 Answers 2

2

Assuming you have this endpoint that produces that JSON data : sample.com/json.php In your json.php

<?php
   echo json_encode($your_json_variable);
?>

And in your script, using Jquery AJAX :

$.getJSON('sample.com/json.php', function(data){

    $.each(data, function(k,v){
     console.log(v);
    });

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

Comments

2

It actually depends on how you want to get it with javascript. If you will get it with ajax you can use it like below to write it out.

<%php echo json_encode($your_json_data);%>

If your javascript is on same file as your json array is than

<script language="javascript">
 var myJSJson = <%php echo json_encode($json_data)%>;
</script>

Would do the trick.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.