Reputation: 1877
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
Upvotes: 1
Views: 3114
Reputation: 1041
Try this one: $.pareseJSON()
here is example:
var json = "<?php echo json_encode($noticesDates); ?>";
jsArray = jQuery.parseJSON(json);
Upvotes: 0
Reputation: 26
use this
var array = JSON.parse("<?php echo json_encode($noticesDates) ?>");
Upvotes: 1
Reputation: 791
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>
Upvotes: 4