I'm having a similar issue to this post where using pg_fetch_all is returning the data I want buried in an associative array. When I run the query in PgAdmin I get the results I expect as a feature collection, so what's the correct way to format my query and PHP to return the result as a geojson object? My aim is to get geojson for a Leaflet map. I'm querying a shapefile of single locations that I want some attributes associated.
Here's my query and code snippet:
$query = "SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features
FROM (SELECT 'Feature' As type
, ST_AsGeoJSON(lg.geom)::json As geometry
, row_to_json((SELECT l FROM (SELECT id, photo, type) As l
)) As properties
FROM public.\"Peng\" As lg ) As f ) As fc;";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
$geodata = pg_fetch_all($result);
print_r($geodata);
While I'm sure I could drill down into the object, there's got to be a cleaner way to get a geojson string to the client.
So, in this case it's just a matter of correctly accessing the values in the array with $geodata[0]['row_to_json']