简体   繁体   English

将 PHP 数组转换为 JavaScript 对象

[英]Converting PHP array to JavaScript Object

I'm having trouble here.我在这里遇到了麻烦。 There seems to be something wrong due to my lack of knowledge in JSON and JS: Here's my little code:由于我对 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 ++;
} 

and I am storing the SQL data on a php array directly after retrieving the row.我在检索行后直接将 SQL 数据存储在 php 数组中。 Now, using JSON approach, here was my trial but apparently it isn't loading or working.现在,使用 JSON 方法,这是我的试用版,但显然它没有加载或工作。

var
    mapObject,
    markers = [],
    markersData = JSON.parse( '<?php echo json_encode($markersData); ?>' );

And I'm looking for the converted php array to do the same job as this JS code:我正在寻找转换后的 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'
            }
            ]
        };

What am I doing wrong here?我在这里做错了什么?

EDIT: A few folks asked me to show them the output.编辑:一些人要求我向他们展示输出。 It's available here: http://pastebin.com/7Ebz2GzP可在此处获得:http: //pastebin.com/7Ebz2GzP

You should use json_encode to send the data to javascript like您应该使用json_encode将数据发送到 javascript 之类的

echo json_encode($markersData);

and then in javascript you can use JSON.parse to make it a javascript object然后在 javascript 中你可以使用 JSON.parse 使它成为一个 javascript 对象

response = JSON.parse(markersData, function(k,v){
                    return v;
                });
    console.log(response);

Use json_encode :使用json_encode

echo json_encode($markersData);

In JS parse it:在 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