PHP数组到JavaScript [英] php array to javascript

查看:68
本文介绍了PHP数组到JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

将PHP字符串传递给Javascript变量(包括转义换行符)

我有一个要在JavaScript脚本中使用的PHP数组。我想我将不得不将其转换为javascript可以理解的字符串,并将该字符串嵌入脚本中。

I have a PHP array that I want to use in a javascript script. I suppose that I will have to convert it to a string that can be understood by javascript, and embed the string inside the script. What is the best way to accomplish this.

编辑-这是页面的初始加载,而不是随后的AJAX调用

EDIT - This is for initial load of the page, not subsequent AJAX call

$textMessages = array();


推荐答案

json_encode()。放入PHP数组中,接收JS数组的字符串表示形式。

json_encode(). Put in a PHP array, receive a string representation of a JS array.

这篇关于PHP数组到JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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