将php数组重新排列为javascript数组问题
我有一组像这样的 PHP 数组数据:
$xx_add = array(
array('id'=>1, 'name'=>"jiahui shop", 'description' => "Jl. Sukarjo Wiryopranoto No. 69 A, Pasar Baru", 'lat'=>-6.249650, 'lng'=>106.850288),
array('id'=>2, 'name'=>"Success United Pte Ltd", 'description' =>"Atrium Plaza Blok E Unit 122 5 Jl. Senen Raya No. 135 Senen, Jakarta Pusat", 'lat'=>-6.167170, 'lng'=>106.873894),
array('id'=>3, 'name'=>"Chilis Restaurant, Lt. 2, Jl. MH. Thamrin No. 11, Sarinah", 'description' =>"Sarinah Building, Lt. 2, Jl. MH. Thamrin No. 11, Sarinah", 'lat'=>-6.197090, 'lng'=>106.738213)
);
我该怎么做,以便我可以获得一组 JavaScript 数组,如下所示?
var $xx_add = [
{'id': 1, 'name': 'jiahui shop', 'description': 'Jl. Sukarjo Wiryopranoto No. 69 A, Pasar Baru', 'lat': -6.249650, 'lng': 106.850288 },
{'id': 2, 'name': 'Success United Pte Ltd', 'description': 'Atrium Plaza Blok E Unit 122 5 Jl. Senen Raya No. 135 Senen, Jakarta Pusat', 'lat': -6.167170, 'lng': 106.873894 },
{'id': 3, 'name': 'Chilis Restaurant', 'description': 'Sarinah Building, Lt. 2, Jl. MH. Thamrin No. 11, Sarinah', 'lat': -6.197090, 'lng': 106.738213 }
];
谢谢
I have a group of PHP array data like this:
$xx_add = array(
array('id'=>1, 'name'=>"jiahui shop", 'description' => "Jl. Sukarjo Wiryopranoto No. 69 A, Pasar Baru", 'lat'=>-6.249650, 'lng'=>106.850288),
array('id'=>2, 'name'=>"Success United Pte Ltd", 'description' =>"Atrium Plaza Blok E Unit 122 5 Jl. Senen Raya No. 135 Senen, Jakarta Pusat", 'lat'=>-6.167170, 'lng'=>106.873894),
array('id'=>3, 'name'=>"Chilis Restaurant, Lt. 2, Jl. MH. Thamrin No. 11, Sarinah", 'description' =>"Sarinah Building, Lt. 2, Jl. MH. Thamrin No. 11, Sarinah", 'lat'=>-6.197090, 'lng'=>106.738213)
);
How can I do, so that I can get a bundle of javascript array as like shown below?
var $xx_add = [
{'id': 1, 'name': 'jiahui shop', 'description': 'Jl. Sukarjo Wiryopranoto No. 69 A, Pasar Baru', 'lat': -6.249650, 'lng': 106.850288 },
{'id': 2, 'name': 'Success United Pte Ltd', 'description': 'Atrium Plaza Blok E Unit 122 5 Jl. Senen Raya No. 135 Senen, Jakarta Pusat', 'lat': -6.167170, 'lng': 106.873894 },
{'id': 3, 'name': 'Chilis Restaurant', 'description': 'Sarinah Building, Lt. 2, Jl. MH. Thamrin No. 11, Sarinah', 'lat': -6.197090, 'lng': 106.738213 }
];
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否正在尝试从 PHP 获取 JSON 代码?使用 json_encode() 函数:
http://php. net/manual/en/function.json-encode.php
然后您需要添加变量声明。 :)
Are you trying to get JSON code from PHP? Use
json_encode()
function:http://php.net/manual/en/function.json-encode.php
Then you'll need to add the variable declaration. :)
转换为 JSON:
请参阅 http://php.net/manual/en/function。 json-encode.php
Convert to JSON:
See http://php.net/manual/en/function.json-encode.php
我使用json_encode。
I use json_encode.
你还可以将 PHP 嵌入到 JavaScript 块中(这是我能想到的最丑陋的解决方案):
You can also embed PHP in your JavaScript block (which is the most ugly solution I can think of):
JSON 编码
JSON encode it