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

2
  • how are you saving those arrays? Commented Nov 6, 2012 at 7:20
  • The "related" column (which was shown to you when you entered your question title) has all the answers. Commented Nov 6, 2012 at 7:21

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

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.