[英]JSON from PHP – nested arrays
我正在从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"
}]
嵌套数组很容易创建。 这是一个例子:
$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",
}
}
有一种简单的方法可以解决这个问题。 只需以示例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',
),
)
例如,如果您想对示例代码的经/纬度进行编码,则为:
$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]
}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.