2015-05-13 167 views
2

我从PHP输出一些JSON,但我有困难,了解如何嵌套数组(至少我认为这是它叫什么)JSON从PHP - 嵌套数组

我可以输出单集,例如,"type": "Feature"但我会怎么做

"geometry": { 
    "type": "Point", 
    "coordinates": [-77.03238901390978,38.913188059745586] 
}, 

例如,对于一个项目的JSON数组中所需的输出可能是:

{ 
    "type": "Feature", 
    "geometry": { 
     "type": "Point", 
     "coordinates": [-77.03238901390978,38.913188059745586] 
    }, 
    "properties": { 
     "title": "Mapbox DC", 
     "description": "1714 14th St NW, Washington DC", 
     "marker-color": "#fc4353", 
     "marker-size": "large", 
     "marker-symbol": "monument" 
    } 
}, 

而且到目前为止我的代码看起来升IKE此:

<?php 
$projects = $pages->find('template=project-detail, sort=sort'); 
$projects_array = array(); 

foreach ($projects as $project) { 

    $title = $project->title; 
    $long = $project->project_location_marker_long; 
    $lat = $project->project_location_marker_lat; 

    $projects_array[] = array(
     'title' => $title 
    ); 

} 

$projects_json = json_encode($projects_array, true); 
?> 
<script> 
var geojson = <?php echo echo $projects_json; ?> 
</script> 

产生类似如下:

[{ 
    "title": "Steel Strike 1980" 
}, { 
    "title": "Chapel Flat Dyke Boat" 
}] 
+0

为什么你将'true'作为'json_encode'的第二个参数?该函数的第二个参数是一个被常量填充的选项。有关更多信息,请参阅http://php.net/manual/en/function.json-encode.php。 也许你正在考虑'json_decode',它需要第二个参数来返回一个关联数组。 –

回答

3

嵌套阵列是简单的创建。这里有一个例子:

$my_array = array(
    'string_example' => 'asdf', 
    'integer_example' => 42, 
    'array_example' => array() // this array is nested 
); 

在这个嵌套数组中,你可以放任何你想要的东西。例如,让我们把同样的事情在里面:

$my_array = array(
    'string_example' => 'asdf', 
    'integer_example' => 42, 
    'array_example' => array(
     'string_example' => 'asdf', 
     'integer_example' => 42, 
     'array_example' => array() 
    ) 
); 

所以从您的代码示例的工作,这里是一个开始,给你包含在数据:

foreach ($projects as $project) { 

    $title = $project->title; 
    $long = $project->project_location_marker_long; 
    $lat = $project->project_location_marker_lat; 

    $projects_array[] = array(
     'geometry' => array(
      'coordinates' => array($long, $lat) 
     ) 
     'properties' => array(
      'title' => $title 
     ) 
    ); 

} 

这将导致以下json时编码:

{ 
    "geometry": { 
     "coordinates": [-77.03238901390978,38.913188059745586] 
    }, 
    "properties": { 
     "title": "Mapbox DC", 
    } 
} 
0

有一个简单的方法来解决这个问题。只要看看你的榜样JSON,对其进行解码并查看输出结果是什么样子:

<?php 
$json = ' 
{ 
    "type": "Feature", 
    "geometry": { 
     "type": "Point", 
     "coordinates": [-77.03238901390978,38.913188059745586] 
    }, 
    "properties": { 
     "title": "Mapbox DC", 
     "description": "1714 14th St NW, Washington DC", 
     "marker-color": "#fc4353", 
     "marker-size": "large", 
     "marker-symbol": "monument" 
    } 
}'; 
var_export(json_decode($json, true)); 

输出:

array (
    'type' => 'Feature', 
    'geometry' => 
    array (
    'type' => 'Point', 
    'coordinates' => 
    array (
     0 => -77.032389013909778, 
     1 => 38.913188059745586, 
    ), 
), 
    'properties' => 
    array (
    'title' => 'Mapbox DC', 
    'description' => '1714 14th St NW, Washington DC', 
    'marker-color' => '#fc4353', 
    'marker-size' => 'large', 
    'marker-symbol' => 'monument', 
), 
) 
0

如果你喜欢编码例如中纬度/经度示例代码这将是:

$title = $project->title; 
$long = $project->project_location_marker_long; 
$lat = $project->project_location_marker_lat; 

$projects_array[] = array(
    'title' => $title, 
    'coordinates' => array($lat,$lon) 
); 

这将导致这样的事情:

[{ 
    "title": "Steel Strike 1980", 
    "coordinates": [-77.03238901390978,38.913188059745586] 
}, { 
    "title": "Chapel Flat Dyke Boat", 
    "coordinates": [-77.03238901390978,38.913188059745586] 
}]