将PHP数组转换为JavaScript对象
Converting PHP array to JavaScript Object
我遇到麻烦了!由于我对JSON和JS缺乏了解,似乎出现了一些问题。这是我的小代码:
$markersData = array();
$x = 0;
while ($row = @mysqli_fetch_assoc($result))
{
$type = $row['type'];
$markersData[$type][$x]['name'] = $row['name'];
$markersData[$type][$x]['location_latitude'] = $row['location_latitude'];
$markersData[$type][$x]['location_longitude'] = $row['location_longitude'];
$markersData[$type][$x]['map_image_url'] = '';
$markersData[$type][$x]['name_point'] = $row['name_point'];
$markersData[$type][$x]['description_point'] = $row['description_point'];
$markersData[$type][$x]['url_point'] = $global['rootURI'] . '/view.php?id=' . $row['id'];
$x ++;
}
在检索行之后,我将SQL数据直接存储在php数组中。现在,使用JSON方法,这是我的试用版,但显然它没有加载或工作。
var
mapObject,
markers = [],
markersData = JSON.parse( '<?php echo json_encode($markersData); ?>' );
我正在寻找转换后的php数组来完成与此JS代码相同的工作:
var
mapObject,
markers = [],
markersData = {
'Shop': [
{
name: 'Bondi Beach',
location_latitude: 43.119445,
location_longitude: 131.881006,
map_image_url: 'img/img.png',
name_point: 'Vladivostok',
description_point: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard',
url_point: '02.html'
}
],
'Cinema': [
{
name: 'Bondi Beach',
location_latitude: 43.124034,
location_longitude: 131.883517,
map_image_url: 'img/img.png',
name_point: 'Vladivostok',
description_point: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard',
url_point: '02.html'
},
{
name: 'Coogee Beach',
location_latitude: 43.126117,
location_longitude: 131.877423,
map_image_url: 'img/img2.png',
name_point: 'Matart Group',
description_point: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard',
url_point: '02.html'
}
]
};
我在这里做错了什么?
编辑:有几个人让我给他们看输出。可在此处获取:http://pastebin.com/7Ebz2GzP
您应该使用json_encode
将数据发送到类似的javascript
echo json_encode($markersData);
然后在javascript中,您可以使用JSON.parse使其成为javascript对象
response = JSON.parse(markersData, function(k,v){
return v;
});
console.log(response);
使用json_encode:
echo json_encode($markersData);
在JS中解析它:
res = JSON.parse(markersData, function(a,b){ return b;});
alert(res);
相关文章:
- Chrome开发工具(如何知道我在调用哪个javascript对象)
- 循环遍历以数组为值的Javascript对象
- 从ajax请求中获取javascript对象
- 如何从对象的原型方法访问JavaScript对象属性
- 将XML转换为普通的旧JavaScript对象
- 通过引用传递JavaScript对象
- javascript对象操作:根据指定条件选择属性
- Javascript对象类在单击时打开窗口进行颜色选择,并在更改时替换对象背景颜色
- 如何在异步函数中使用javascript对象
- 临时Javascript对象
- 如何在ASP中为用户控件添加Javascript对象网
- 使用数组向下搜索Javascript对象
- Rails将JavaScript对象存储到Model的有效方式
- JavaScript对象不是从原型链继承的
- 如何创建具有默认值的JavaScript对象字段?(AngularJS模型相关)
- SetInterval在javascript对象中表现怪异
- Javascript 对象和 this 关键字
- 如何在不知道关键字的情况下访问javascript对象值
- 在 JavaScript 对象中设置要使用的运算符的属性
- 如何搜索JavaScript对象并更改值