I am using a CMS, and want to pass some PHP arrays to JSON. I am managing to do this fine but the issue is that it wraps [] round the array, whereas I need it without [] to use it as geojson within Mapbox (https://www.mapbox.com/mapbox-gl-js/example/popup-on-click/).
<?php
$programme_array = array();
$programmes = $pages->find('parent=programme, sort=sort');
foreach ($programmes as $programme) {
$title = $programme->title;
$url = $programme->url;
$summary = $programme->programme_summary;
$image = $programme->programme_venue_image->url;
$long = $programme->programme_location->lng;
$lat = $programme->programme_location->lat;
$programme_array[] = array(
'type' => 'Feature',
'geometry' => array(
'type' => 'Point',
'coordinates' => [$long,$lat]
),
'properties' => array(
'title' => $title,
'description' => $summary,
'image' => $image,
'url' => $url,
"marker-symbol" => "music"
),
);
}
$programme_json = json_encode($programme_array, true);
?>
[{"type":"Feature","geometry":{"type":"Point","coordinates":["-1.466439","53.376842"]},"properties":{"title":"Site Gallery","description":"Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Donec id justo. Aenean tellus metus, bibendum sed, posuere ac, mattis non, nunc. Suspendisse feugiat. Etiam rhoncus.","image":"\/Freelance\/art-sheffield-2016\/site\/assets\/files\/1032\/site_gallery.jpg","url":"\/Freelance\/art-sheffield-2016\/programme\/site-gallery\/","marker-symbol":"music"}},{"type":"Feature","geometry":{"type":"Point","coordinates":["-1.477881","53.374798"]},"properties":{"title":"Moore Street Substation","description":"","image":null,"url":"\/Freelance\/art-sheffield-2016\/programme\/moore-street-substation\/","marker-symbol":"music"}},{"type":"Feature","geometry":{"type":"Point","coordinates":["-1.459620","53.380562"]},"properties":{"title":"S1 Artspace","description":"","image":null,"url":"\/Freelance\/art-sheffield-2016\/programme\/s1-artspace\/","marker-symbol":"music"}}]
UPDATE
I have come up with the following, which works in terms of formatting BUT it's only returning one item and not three?
$geojson = array( 'type' => 'FeatureCollection', 'features' => array());
$programmes = $pages->find('parent=programme, sort=sort');
foreach ($programmes as $programme) {
$marker = array(
'type' => 'Feature',
'properties' => array(
'title' => $programme->title,
"marker-symbol" => "music"
),
'geometry' => array(
'type' => 'Point',
'coordinates' => array(
$programme->programme_location->lng,
$programme->programme_location->lat
)
)
);
array_push($geojson['features'], $marker);
}
$programme_json = json_encode($marker, JSON_PRETTY_PRINT);
What's even weirder is that if I add the square brackets [ ]
to $marker = array(
effectively making it $marker[] = array(
then it returns all the items BUT it has the square brackets on it.
I'm so deep down the rabbit hole...
This is what I ended up with after much trial and error.
<?php
$geojson = array(
'type' => 'FeatureCollection',
'features' => array()
);
$programmes = $pages->find('parent=programme, sort=sort');
foreach ($programmes as $programme) {
$marker = array(
'type' => 'Feature',
'properties' => array(
'title' => $programme->title,
'url' => $programme->url,
'summary' => $programme->programme_summary,
'image' => $programme->programme_venue_image->url
),
'geometry' => array(
'type' => 'Point',
'coordinates' => array(
$programme->programme_location->lng,
$programme->programme_location->lat
)
)
);
array_push($geojson['features'], $marker);
}
$programme_json = json_encode($geojson);
?>