1

i have array in php, and i need work with this array in jQuery. What best way send array( or variable ) from php to jQuery? Thanks

2 Answers 2

7

json_encode() (PHP >= 5.2) is arguably the easiest way.

$array_js = json_encode($array);

echo "<script type='text/javascript'> my_array = ".$array_js."; </script>";
Sign up to request clarification or add additional context in comments.

1 Comment

If you're (PHP < 5.2), I've had success with the jsonwrapper script. boutell.com/scripts/jsonwrapper.html
1

If you don’t have to dynamically load it, try something like this:

<script>
var the_array = <?php echo json_encode($array); ?>
</script>

Comments

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.