如何从数据库获取值并将其放在json_encoded的php文件中的特定括号中?

问题描述 投票:0回答:1

这是我的代码:

<?php 

session_start();

$con = mysqli_connect('localhost', 'id128XXXXX', 'LXXXX');

mysqli_select_db($con, 'idXXXXXX');

$query = "select * from sensor order by id desc limit 1";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);


$value = array( 
    "geometry"=>array( 
        "type"=> "Point", 
        "coordinates"=> [0,0] ///I want to get values from a database and put it in this bracket.
    ) , 

    "type"=>"Feature", 
    "properties" => (object) array()
); 

// Use json_encode() function 
$json = json_encode($value); 

// Display the output 
echo($json); 

?> 

关于此的任何帮助将非常感谢,谢谢。至于我尝试过的。我尝试使用

 "coordinates"=> [$row[longitude],$row[lat]]

最终给了我这种类型的输出:

{"geometry":{"type":"Point","coordinates":["0","0"]},"type":"Feature","properties":{}} 

我需要将[“ 0”,“ 0”]设置为[0,0],因为带有引号的地图标记将不会显示。如果有人问我正在使用mapbox,并且我需要输出此json数据以使地图标记保持刷新。

php json mapbox geojson
1个回答
0
投票

将结果转换为整数:

"coordinates"=> [intval($row['longitude']), intval($row['lat'])]

并且不要忘了用字符串longitudelat引用。

© www.soinside.com 2019 - 2024. All rights reserved.