Search code examples
phphtmljsonperformancegeojson

I have this external geojson file. i would like to loop and echo the properties object (e.g "Name" "Easting" "Northing"). i dont were am getting wrong


{ "type": "FeatureCollection", "name": "Trial",

"features": [ { "type": "Feature", "properties": { "NAME": "Muwaya Pri. Sch", "Easting": 686607.293, "Northing": 8561595.506 }, "geometry": { "type": "Point", "coordinates": [ 686607.293, 8561595.50599999912 ] } },

{ "type": "Feature", "properties": { "NAME": "Munkulungwe Pri. Sch", "Easting": 690206.558, "Northing": 8550186.399 }, "geometry": { "type": "Point", "coordinates": [ 690206.558, 8550186.399 ] } },

{ "type": "Feature", "properties": { "NAME": "Fiwale Pri.Sch", "Easting": 685977.67, "Northing": 8539029.88 }, "geometry": { "type": "Point", "coordinates": [ 685977.67, 8539029.88 ] } },

{ "type": "Feature", "properties": { "NAME": "Mwatishi School", "Easting": 702707.723, "Northing": 8554903.974 }, "geometry": { "type": "Point", "coordinates": [ 702707.723, 8554903.97399999946 ] } } ] }

CODE

              <?php$getfile = file_get_contents('test.Geojson');  ?>
              <?php$jsonfile = json_decode($getfile);  ?>

           <?php foreach ($jsonfile->features as $index => $obj): ?>
            <?php echo $obj->Name; ?>
            <?php echo $obj->Northing; ?>
           <?php echo $obj->Eastinglink; ?>

Solution

  • All required properties are under properties key. change your code to read below.

    echo $obj->properties->NAME ;
    echo $obj->properties->Northing; 
    echo $obj->properties->Eastinglink;