문제

I had a function in 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);

that outputs:

    "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"
                }
            }

Now I have rewritten the array to become an object like this:

    //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);

But this changes the output to:

 "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"
                        }
                    }
                }
            },

Which makes it unusable as geoJSON for OpenLayers. Why does the json_encode function behave this way? Can I turn off the setting of the index numbers? Is this a possible little bug?

도움이 되었습니까?

해결책

json_encode will display the same behaviour with any object, even ones implementing the ArrayAccess interface as ArrayObject does; the public properties are used.

To get the behaviour that you want you should pass it an actual array which can be retrieved by calling ArrayObject::getArrayCopy() (or you can cast the object to an array).

echo json_encode($newsensors->getArrayCopy());

다른 팁

Had to convert data trees that contain ArrayObject at different level so i wrote a piece of code to handle and convert every ArrayObject before outputting json, hope it helps : https://github.com/nfroidure/Rest4/blob/master/php/class.Json.php

Use the JsonSerializable Interface (PHP 5 >= 5.4.0, PHP 7) and you should be safe:

public function jsonSerialize()
{
    return $this->getArrayCopy();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top