1

I have a multidimensional array, here:

$noticeDate = json_encode( $noticesDates );

and I want to pass the array into javascript:

var unavailableDates[] = $noticeDate;

Both variables are in the same php file so there is little point using $.getJSON, which basically looks for the variable in an external file. However, how do I pass the object into the javascript array in the same script.

Cheers

3 Answers 3

4

You cant directly assign php variables to js, but you can use something like that:

<script>
  var unavailableDates = jQuery.parseJSON('<?php echo json_encode($noticeDates) ?>');
</script>
3
  • @Christoph's solution lacks the client-side JSON parsing that is included here. This is the more complete solution. Commented Mar 29, 2012 at 12:37
  • var unavailableDates = jQuery.parseJSON('<?php echo json_encode($noticesDates) ?>'); document.write(unavailableDates); doesnt seem to print it out Commented Mar 29, 2012 at 13:33
  • Why do you want to print it out? It's an object you can't just simply print it. If that's your goal you should use a different format imho. Commented Mar 29, 2012 at 13:39
1

use this

var array = JSON.parse("<?php echo json_encode($noticesDates) ?>");
0

Try this one: $.pareseJSON()

here is example:



var json = "<?php echo json_encode($noticesDates); ?>";

jsArray = jQuery.parseJSON(json);

1
  • How does this even attempt to answer the question? Please change your answer to something more resembling one. Commented Mar 29, 2012 at 15:54

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.