繁体   English   中英

将 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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