Home  >  Article  >  Backend Development  >  How to convert php array to js array

How to convert php array to js array

Guanhui
GuanhuiOriginal
2020-05-07 14:12:203783browse

How to convert php array to js array

How to convert php array to js array

First use "json_encode" to convert the php array into a json string;

$arr = ['xiaoming', 'dahuang', 'xiaohua', 'zhangsan'];
$arr = json_encode($arr);

Then use the php syntax in the js code to output the json string;

 var arr = <?php echo $arr?>;

Finally, use the js variable to receive the json string output by the php code, and the js variable is the converted array.

Code:



Print result:

How to convert php array to js array

The above is the detailed content of How to convert php array to js array. For more information, please follow other related articles on the PHP Chinese website!

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