bobo2000
bobo2000

Reputation: 1877

Passing a php array into javascript using JSON

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

Answers (3)

ShirazITCo
ShirazITCo

Reputation: 1041

Try this one: $.pareseJSON()

here is example:



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

jsArray = jQuery.parseJSON(json);

Upvotes: 0

bescht
bescht

Reputation: 26

use this

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

Upvotes: 1

erdeszt
erdeszt

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

Related Questions