Home >php教程 >PHP源码 >php向javascript传递数组的方法

php向javascript传递数组的方法

PHP中文网
PHP中文网Original
2016-05-25 17:10:131589browse

PHP进行数组传值给javascript 此方法适用php5.2以上,php5.2以上自带该类。低于5.2的,请下载json类。json是一种数据传输格式 

php代码

$arr=一个数组;
$str=json_encode($arr);
echo "<input type=&#39;button&#39; name=&#39;insert&#39; value=&#39;Insert&#39; onClick=&#39;add($str)&#39;>";

js代码

function add(array)
{
alert(array.length);
//array就是$str数组
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn