Note that there are some explanatory texts on larger screens.

plurals
  1. POjson_encode behaving different on arrayObject vs array()
    text
    copied!<p>I had a function in php:</p> <pre><code>//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-&gt;geoJSON(); } echo json_encode($sensors); </code></pre> <p>that outputs: </p> <pre><code> "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" } } </code></pre> <p>Now I have rewritten the array to become an object like this:</p> <pre><code> //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-&gt;geoJSON(); } } } $newsensors = new sensors($epsg); echo echo json_encode($newsensors); </code></pre> <p>But this changes the output to:</p> <pre><code> "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" } } } }, </code></pre> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload