我从PHP输出一些JSON,但我很难理解如何进行嵌套数组(至少我认为这就是它的名称)
我可以输出单个集合,例如"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"
}
},
到目前为止我的代码看起来像这样:
<?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 :(得分: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",
}
}
答案 1 :(得分: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',
),
)
答案 2 :(得分:0)
如果您想编码示例代码的lat / lon,那么它将是:
$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]
}]