使用JSON将php数组传递给javascript [英] Passing a php array into javascript using JSON

查看:97
本文介绍了使用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>

这篇关于使用JSON将php数组传递给javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆