-1

Possible Duplicate:
Best way to transfer an array between PHP and Javascript

I am trying to return a series of arrays from PHP to javascript using AJAX.

I have tried pre-formatting the email addresses as a JSON object and returning that to my JS script and then parsing it as JSON but no luck.

I have a main array called emails, and I want these arrays returned from PHP and to be converted to a JS array, I have tried:

emails = $.makeArray($.parseJSON(email)) ;

But with no luck.

How can I achieve what I want?

Community
  • 1
  • 1
imperium2335
  • 22,038
  • 38
  • 106
  • 184

1 Answers1

1

You should be able to use JSON_encode to pass the array directly from PHP to a Javascript variable:

<?php
    $arr = array(
        array("foo" => "bar")
    );
?>
<script type='text/javascript'>
    var myarray = <?php echo JSON_encode($my_array); ?>;
    alert(myarray[0].foo);
</script>
McGarnagle
  • 98,751
  • 30
  • 222
  • 258