duancong2965 2018-11-19 18:02
浏览 138
已采纳

如何在json中为叶子实时制作正确的数组

I am trying us to leaflet.js realtime. I have it working when I encode the coordinates like below. I have read the instructions and it does not specify how it wants the data but found this in a sample. How do I encode it to print it out as it should and is it possible to include more than one set of coordinates?

Any help would be great, thanks.

my php

$lat = "52.45238209999999";
$lng = "-1.743507099999988";

$str = "{".chr(34)."geometry".chr(34).": {".chr(34)."type".chr(34).": ".chr(34)."Point".chr(34).", ".chr(34)."coordinates".chr(34).": [".$lng.", ".$lat."]}, ".chr(34)."type".chr(34).": ".chr(34)."Feature".chr(34).", ".chr(34)."properties".chr(34).": {}}";

echo $str;

and this is how it should look at the end according to the sample.

{"geometry": {"type": "Point", 
              "coordinates": [-104.53702657476524, 1.4311558884747997]
             }, 
              "type": "Feature", 
              "properties": {}
            }
  • 写回答

1条回答 默认 最新

  • dongman5539 2018-11-19 19:08
    关注

    To get the output your after, it's a case of building up a multidimensional array with the right structure so that you can then json_encode() it.

    $lat = "52.45238209999999";
    $lng = "-1.743507099999988";
    
    $data = ["geometry" => ["type" => "Point",
        "coordinates" => [(float)$lng, (float)$lat]],
        "type" => "Feature",
        "properties" => (object)[]
    ];
    
    echo json_encode($data, JSON_PRETTY_PRINT);
    

    gives...

    {
        "geometry": {
            "type": "Point",
            "coordinates": [
                -1.743507099999988, 52.45238209999999
            ]
        },
        "type": "Feature",
        "properties": {}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站