2

所以我的代码在这里:

    $featurecollection = ("FeatureCollection");

        $test[] = array (
        "type" => $featurecollection,
        $features[] = array($images)

    );

   file_put_contents($cache,json_encode($test));

结果如下json:

[
 {
  "type":"feature",
  "0":[
     [
        {
           "title":"some title",
           "src":"value",
           "lat":"value",
           "lon":"value"
        },
        {
           "title":"some title",
             ...

但是我需要以不同的方式嵌套事物,并且我对应该如何构造 php 数组以获得如下结果感到困惑:

{
"type":"FeatureCollection",
 "features":[

  {
     "type":"Feature",
     "geometry":{
        "coordinates":[
           -94.34885,
           39.35757
        ],
        "type":"Point"
     },
     "properties":{
        "latitude":39.35757,
        "title":"Kearney",
        "id":919,
        "description":"I REALLY need new #converse, lol. I've had these for three years. So #destroyed ! :( Oh well. Can't wait to get a new pair and put my #rainbow laces through. #gay #gaypride #bi #proud #pride #colors #shoes #allstar #supporting ",
        "longitude":-94.34885,
        "user":"trena1echo5",
        "image":"http://images.instagram.com/media/2011/09/09/ddeb9bb508c94f2b8ff848a2d2cd3ece_7.jpg",
        "instagram_id":211443415
     }
  },

php 数组会是什么样子?我被所有东西嵌套但仍然有一个关键值的方式抛弃了。

4

2 回答 2

3

这是我在 PHP 中的表示方式:

array(
    'type' => 'FeatureCollection',
    'features' => array(
        array(
            'type' => 'Feature',
            'geometry' => array(
                'coordinates' => array(-94.34885, 39.35757),
                'type' => 'Point'
            ), // geometry
            'properties' => array(
                // latitude, longitude, id etc.
            ) // properties
        ), // end of first feature
        array( ... ), // etc.
    ) // features
)

因此,要获得该结构,每个特征都必须是一个关联数组:

  • 类型,
  • 几何 - 一个关联数组:
    • 坐标 - 一个索引值数组,
    • 类型
  • properties - 一个关联数组,如纬度、经度、id 等。

像这样的时候,我更喜欢区分列表(array(1, 2, 3))和字典或地图(array('a' => 1, 'b' => 2))的语言。

于 2012-04-15T19:29:04.913 回答
0

使用 PHP 5.4 及更高版本:

$array = [
    'type' => 'FeatureCollection',
    'features' => [
        [
            'type' => 'Feature',
            'geometry' => [
                'coordinates' => [-94.34885, 39.35757],
                'type' => 'Point'
            ], // geometry
            'properties' => [
                // latitude, longitude, id etc.
            ] // properties
        ], // end of first feature
        [] // another feature, and so on
    ] // end of features
];

对于下面的 PHP 脚本:

<?php
header('Content-type=> application/json');
echo json_encode($array);

这是 JSON 输出;

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "coordinates": [
          -94.34885,
          39.35757
        ],
        "type": "Point"
      },
      "properties": []
    },
    []
  ]
}

于 2016-11-23T07:42:59.320 回答