Home >Backend Development >PHP Tutorial >Smarty如何将数组变量赋值给js

Smarty如何将数组变量赋值给js

WBOY
WBOYOriginal
2016-06-23 14:38:17955browse

PHP文件中有类似代码:

$arr = array('A','B','C');
$smarty = new Smarty();
$smarty->assign('arr',$arr);
$smarty->display('test.html');


test.html大概是这样的

<script> <br /> var arr = Smarty中赋值的数组变量; <br /> </script>

===========================================================

怎么做比较简单,求解!


回复讨论(解决方案)


$smarty->assign('js_arr',json_encode($arr));

<script> <br /> var arr = {$js_arr}; <br /> </script>

谢了 非常好!

非常有用,谢谢!

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