How to convert php array to js array

Guanhui
Release: 2023-02-28 22:32:01
Original
3847 people have browsed it

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);
Copy after login

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

 var arr = <?php echo $arr?>;
Copy after login

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

Code:



Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template