Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
 
how are you saving those arrays? –  Sibu Nov 6 '12 at 7:20
 
How does email look like ? –  Willy Nov 6 '12 at 7:20
 
The "related" column (which was shown to you when you entered your question title) has all the answers. –  Pekka 웃 Nov 6 '12 at 7:21

marked as duplicate by Pekka 웃, vascowhite, NullPoiиteя ღ, j0k, Jocelyn Nov 12 '12 at 0:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

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>
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.