我正在尝试将mysql中的空间数据结果转换为json格式,并且我得到相同线串的输出:
`[[-105.00341892242432, 39.75383843460583],[-105.0008225440979, 39.751891803969535]] `
但我需要得到这个:
`[{" coordinates":[[-105.00341892242432, 39.75383843460583], [-105.0008225440979, 39.751891803969535]]}`
有人可以帮我吗?
以下是代码:
<?php
require_once("caroom_db.php");
header("Content-Type: application/json");
//$l1 = '';
$ll1 = array();
//$lt = array();
$json_response = array();
$ll =0;
//echo $l;
$jsonData = '[';
$num_point="SELECT NumPoints(geo_type) FROM line_park WHERE id='2'";
$query = mysqli_query($cn, $num_point) or die (mysqli_error($cn));
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$ll = $row["NumPoints(geo_type)"];
//echo $ll;
}
for ($i=1; $i<=$ll; $i++) {
$l1="SELECT X(PointN(geo_type,$i)), Y(PointN(geo_type,$i)) FROM line_park";
$query = mysqli_query($cn, $l1) or die (mysqli_error($cn));
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$ll1[0] = $row["X(PointN(geo_type,$i))"];
$ll1[1] = $row["Y(PointN(geo_type,$i))"];
array_push($json_response,$ll1);
}
}
echo json_encode($json_response);
fclose($cn);
?>
提前谢谢!
答案 0 :(得分:1)
好的,因为我的评论解决了您的问题,我将其作为答案发布,以便您接受。尝试使用php.net/manual/en/function.json-encode.php将数组转换为json,将数值推入数组会更简单。
要添加coodrinates
,只需执行字符串连接
$jsonData = json_encode($json_response);
$jsonData = '{"coordinates":' . $jsonData . '}';
答案 1 :(得分:0)
使用另一个数组键。例如,
json_encode(array('coordinates' => $response));