我在php中有一个函数:
//simple method with array()
$sensors = array();
$query = "select id, x(transform(wkb_geometry,". $epsg . ")) as lon, y(transform(wkb_geometry,". $epsg . ")) as lat from mytable;";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
while ($row = pg_fetch_assoc($result)) {
//var_dump($row);
$mySensor = new sensor($row['id'],$row['lat'],$row['lon']);
$sensors[] = $mySensor->geoJSON();
}
echo json_encode($sensors);
输出:
"features": [{
"type": "Feature",
"id": 1579028,
"x": 4.85310557823,
"y": 52.7205622103,
"geometry": {
"type": "Point",
"coordinates": [4.85310557823, 52.7205622103],
"crs": {
"type": "OGC",
"properties": {
"urn": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
}
现在我已经将数组重写为这样的对象:
//advanced method with arrayObject:
class sensors extends ArrayObject {
function __construct($epsg){
$query = "select id, x(transform(wkb_geometry,". $epsg . ")) as lon, y(transform(wkb_geometry,". $epsg . ")) as lat from mytable;";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
while ($row = pg_fetch_assoc($result)) {
//var_dump($row);
$mySensor = new sensor($row['id'],$row['lat'],$row['lon']);
$this[] = $mySensor->geoJSON();
}
}
}
}
$newsensors = new sensors($epsg);
echo echo json_encode($newsensors);
但这会将输出更改为:
"features": {
"0": {
"type": "Feature",
"id": 1579028,
"x": 4.85310557823,
"y": 52.7205622103,
"geometry": {
"type": "Point",
"coordinates": [4.85310557823, 52.7205622103],
"crs": {
"type": "OGC",
"properties": {
"urn": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
}
}
},
这使得它无法用作 OpenLayers 的 geoJSON。为什么 json_encode 函数会这样?我可以关闭索引号的设置吗?这是一个可能的小错误吗?