I'm outputting some JSON from PHP, but I'm having difficulty understanding how to do nested arrays (at least I think that is what it is called)
I can output single sets, for example, "type": "Feature" but how would I do
"geometry": {
"type": "Point",
"coordinates": [-77.03238901390978,38.913188059745586]
},
For example, the desired output for one item in the JSON array might be:
{
"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"
}
},
And my code so far looks like this:
<?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>
Which generates something like the following:
[{
"title": "Steel Strike 1980"
}, {
"title": "Chapel Flat Dyke Boat"
}]
trueas a second argument tojson_encode? The second argument for that function is an option indended to be filled by a constant. See php.net/manual/en/function.json-encode.php for more info. Perhaps you're thinking ofjson_decode, which takes a second agrument to return an associative array.