2012-04-15 150 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": [] 
 
    }, 
 
    [] 
 
    ] 
 
}