使用JSON将php数组传递给javascript
问题描述:
我有一个多维数组,这里:
I have a multidimensional array, here:
$noticeDate = json_encode( $noticesDates );
我希望将数组传递给javascript:
and I want to pass the array into javascript:
var unavailableDates [] = $ noticeDate;
var unavailableDates[] = $noticeDate;
这两个变量都在同一个php文件中,所以使用$ .getJSON几乎没什么意义,它基本上是寻找外部文件中的变量。但是,如何将对象传递到同一脚本中的javascript数组中。
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.
干杯
答
你不能直接将php变量分配给js,但你可以使用类似的东西:
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>