Use JSON .(使用JSON 。)
In the following example $php_variable
can be any PHP variable.(在以下示例中, $php_variable
可以是任何PHP变量。)
<script type="text/javascript">
var obj = <?php echo json_encode($php_variable); ?>;
</script>
In your code, you could use like the following:(在您的代码中,您可以使用以下代码:)
drawChart(600/50, <?php echo json_encode($day); ?>, ...)
In cases where you need to parse out an object from JSON-string (like in an AJAX request), the safe way is to use JSON.parse(..)
like the below:(在需要从JSON字符串解析对象的情况下(例如在AJAX请求中),安全的方法是使用JSON.parse(..)
,如下所示:)
var s = "<JSON-String>";
var obj = JSON.parse(s);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…