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
)

したがって、その構造を取得するには、各機能が次の連想配列である必要があります。

  • タイプ、
  • geometry - 以下の連想配列:
    • 座標 - 値のインデックス付き配列、
    • タイプ
  • プロパティ - 緯度、経度、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 に答える