简体   繁体   中英

Convert Php array in javascript array

I have php array like this

[0]->imgae_name=1
 image_url=a

[1]->imgae_name=2
 image_url=b

i want to convert this array in javascript array how can i do this?

json_encode($your_array);

is the right way to do this. It converts your php array to this string:

[{imgae_name:1,image_url:"a"},{imgae_name:2,image_url:"b"}]

Then you only have to assign it to a variable in a script tag.

您可以使用json_encode将PHP数组转换为JSON,然后将json字符串回显到页面中的JavaScript中,然后使用Javascript JSONObject将其转换为JS数组

In php

<?
$array = array("A" => "1", "B" => "2");
echo json_encode($array);
?>

In Javascript

var mObject = eval("(" + phpecho + ")");

Then you can access the value :

 mObject.A // 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM