json_decode($data) PHP

Asked

Viewed 104 times

1

I’m using the json_decode on top of a Geojson file. All I need is to print on the screen a Feature specific to 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 on the screen a specific value?

I tried to:

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

But I couldn’t do it.

2 answers

1

Hi! Maybe the way I did below can 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.

  • Thank you so much shoogie!! Solved 100%, I was already days with this question!

  • You’re welcome, I’m glad it worked out!!! D

1


You can use the function json_decode passing the second parameter as true so that all may be converted to array 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'];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.