0

I am having issues creating this "features object" in php/json here is an example of this code: https://doc.arcgis.com/en/arcgis-online/reference/geojson.htm

I am getting all of the information in the inside of the Features Object array and getting it formatted just fine but I can't seem to get the inital features object created.

 {
  "type": "FeatureCollection",
  **"features": [**
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [102.0, 0.5]
      },
      "properties": {
        "prop0": "value0"
      }
    }
  ]
}

my code is coming out like this though in json format:

{
  "type": "FeaturedCollection",
  "0": {
    **"features": {**
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": []
      }
    },
    "properties": {
      "id": 282
    }
  },
  "1": {
    "features": {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": []
      }
    },
    "properties": {
      "id": 280
    }
  },

This is the code I am using to create the array:

foreach($posts as $post) {
        $data[] = array (
            'features' => array (
                'type' => 'Feature',
                'geometry' => array (
                    'type' => 'Point',
                    'coordinates' => array (
                        ),
                    ),
                ),
            
                'properties' => array(
                    'id' => $post->ID,
                
                ),
            );
    }

    return $data;

can someone point me in the right direction of how to create this Features Object? Again I am coding this in PHP and rendering it as JSON or GeoJson.

I found the answer: In case someone else is looking for the answer on how to structure geojson code in php this example works.

<?php

// Sample data (e.g., from a database)
$locations = array(
    array("name" => "Location A", "latitude" => 34.0522, "longitude" => -118.2437),
    array("name" => "Location B", "latitude" => 40.7128, "longitude" => -74.0060),
);

// Create the GeoJSON FeatureCollection structure
$geojson = array(
    'type' => 'FeatureCollection',
    'features' => array()
);

// Iterate through locations and create GeoJSON features
foreach ($locations as $location) {
    $feature = array(
        'type' => 'Feature',
        'geometry' => array(
            'type' => 'Point', // GeoJSON geometry type
            'coordinates' => array($location['longitude'], $location['latitude']) // GeoJSON coordinates format
        ),
        'properties' => array(
            'name' => $location['name'] // Add properties as needed
        )
    );
    // Add the feature to the FeatureCollection
    array_push($geojson['features'], $feature);
}

// Set the header to indicate JSON output
header('Content-type: application/json');

// Encode the PHP array as a JSON string and output it
echo json_encode($geojson, JSON_PRETTY_PRINT);

?>
3
  • Not sure I understand the question, but for one, the arcgis object format is { "type": "FeatureCollection", "features": [ ...an array of objects... ]} and that is not what your code is building. You're just tacking objects on using their array index. Commented Jun 30 at 21:51
  • @Mike'Pomax'Kamermans, correct. I need to get my code to be { "type": "FeatureCollection", "features": [ ...an array of objects... ]}, I was just trying to show what I was initially doing. So that is what I am asking help for an example of how I can get my code in this format: { "type": "FeatureCollection", "features": [ ...an array of objects... ]} Commented Jun 30 at 21:55
  • 3
    You just... literally do that? Create an object that models this data as per the spec, and then convert that to JSON. E.g. stackoverflow.com/a/9858457/740553 - don't put arrays in arrays, use StdClass and assign properties, unless you actually need an array as value. Commented Jun 30 at 23:42

1 Answer 1

0

You can make the objects like this. Note that the features element is an array, but I'm casting it to an object as required:

foreach ($posts as $post) {
    $data[] = array(
        'features' => array(
            (object)[
                'type' => 'Feature',
                'geometry' => array(
                    'type' => 'Point',
                    'coordinates' => array(),
                 )
            ]
        ),

        'properties' => array(
            'id' => $post->ID,

        ),
    );
}

return $data;
Sign up to request clarification or add additional context in comments.

5 Comments

This worked for the most part it displays in json: Using that answer as is returned these values in JSON: ``` { "type": "FeaturedCollection", "0": { "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [] }, "properties": { "id": 282 } } ] }, ```
"Features" has an array around it the only repeating part of the json object/array should be this: ``` { "type": "Feature", "geometry": { "type": "Point", "coordinates": [] }, "properties": { "id": 282 } } ```
I need the format like this: { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [102.0, 0.5] }, "properties": { "prop0": "value0" } } ] }
look in the php official documentation to see how to make this array : php.net/array
@kmoser - how can i get the Features object on the outside of the array? so that way the array loop is looping like this: I need the format like this: { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [102.0, 0.5] }, "properties": { "prop0": "value0" } }, { "type": "Feature", "geometry": { "type": "Point", "coordinates": [103.0, 0.5] }, "properties": { "prop0": "value0" } } ] }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.