json_decode ($ data) PHP [duplicate]

1

I am using json_decode on top of a GeoJSON file. All I need is to print a specific feature of the file, which has the following structure:

var GEOJSON = {

"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "properties": { "ID": "02", "NOME": "xxxxxxxxx", "TIPO": "xxxxxx", " }, "geometry": { "type": "Point", "coordinates": [ -90.00012544789, -47.012254699888 ] } }, (...)

I have several points with the structure shown, but how do I print a specific value on the screen?

I tried:

echo $tr->features[0]->properties->ID;

But I could not.

    
asked by anonymous 10.06.2017 / 06:13

2 answers

1

You can use the json_decode function by passing the second parameter as true to that all be converted to% associative%.

$geojson = '{"type": "FeatureCollection","crs": {"type": "name","properties": {"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},"features": [{"type": "Feature","properties": {"ID": "02","NOME": "xxxxxxxxx","TIPO": "xxxxxx"},"geometry": {"type": "Point","coordinates": [ -90.00012544789, -47.012254699888 ]}}]}';
$geojson_array = json_decode($geojson, true);

echo $geojson_array['features'][0]['properties']['ID'];
    
12.06.2017 / 18:05
1

Hi! Maybe the way I did it below might help you.

$geojson = '{"type": "FeatureCollection","crs": {"type": "name","properties": {"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},"features": [{"type": "Feature","properties": {"ID": "02","NOME": "xxxxxxxxx","TIPO": "xxxxxx"},"geometry": {"type": "Point","coordinates": [ -90.00012544789, -47.012254699888 ]}}]}';
$geojson_array = json_decode($geojson);
$geojson_array = (array)$geojson_array;

echo $geojson_array["features"][0]->properties->ID;

die;

Hug.

    
10.06.2017 / 20:24