2012-04-15 192 views
2

所以我的代碼在這裏:嵌套的PHP數組JSON

$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數組看起來像什麼?我被一切都嵌套但仍然有一個關鍵價值的方式拋棄。

+0

我*想*您只需要更換'$功能[] =數組($圖片)'和' '特點'=>陣列($圖片)',不過我'm pretty n00b w/PHP ... – 2012-04-15 19:25:45

回答

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 
) 

因此,要獲得該結構中,每個特徵必須是一個關聯數組:

  • 類型,
  • 幾何 - 一個關聯數組:
    • 座標 - 一個索引值的數組,
    • 類型
  • 性質 - 如緯度,經度,ID值等的關聯數組

它的時間等,這些當我寧願列表(array(1, 2, 3))和字典或地圖之間區分語言(array('a' => 1, 'b' => 2) )。

+0

謝謝!現在開始工作...... – 2012-04-15 19:38:40

+0

好吧,我如何循環這些變量(創建多個功能)?我得到如何構建這個數組,然後將其編碼爲json,我只是難以解決如何使用多個實例來構建它。 – 2012-04-17 10:24:39

+1

你從哪裏獲得數據(以及以何種結構)?我會用這個信息創建一個新的問題來獲得更快的響應:) – Ross 2012-04-17 12:35:02

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 
    [] // etc. 
] // features 
]; 

下面是使用標題下方 頭( '內容類型=>應用/ JSON')的JSON輸出; echo json_encode($ array);

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