a-ok Posted February 23, 2016 Posted February 23, 2016 Hi folks, I 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"}}] Any thoughts? I know this is perhaps more PHP/JSON related question than a PW question but I just wanted to make sure I had this set up correctly.
DaveP Posted February 23, 2016 Posted February 23, 2016 From the docs it looks like JSON_FORCE_OBJECT might solve that.
adrian Posted February 23, 2016 Posted February 23, 2016 Here is some info to help explain what is going on and how to fix it: http://stackoverflow.com/questions/15559735/no-square-bracket-json-array http://stackoverflow.com/questions/7109424/remove-the-brackets-in-json Also, I am curious about the "true" option you are passing - I don't think that is valid, although it may not do any harm either.
a-ok Posted February 23, 2016 Author Posted February 23, 2016 Thanks for the replies. From the docs it looks like JSON_FORCE_OBJECT might solve that. Unfortunately I tried this and it outputs the following (no need for ... "0" or "1" etc) {"0":{"type":"Feature","geometry":{"type":"Point","coordinates":{"0":"-1.466439","1":"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"}},"1":{"type":"Feature","geometry":{"type":"Point","coordinates":{"0":"-1.477881","1":"53.374798"}},"properties":{"title":"Moore Street Substation","description":"","image":null,"url":"\/Freelance\/art-sheffield-2016\/programme\/moore-street-substation\/","marker-symbol":"music"}},"2":{"type":"Feature","geometry":{"type":"Point","coordinates":{"0":"-1.459620","1":"53.380562"}},"properties":{"title":"S1 Artspace","description":"","image":null,"url":"\/Freelance\/art-sheffield-2016\/programme\/s1-artspace\/","marker-symbol":"music"}}}
a-ok Posted February 23, 2016 Author Posted February 23, 2016 I have come up with the following, which works in terms of formatting BUT it's only returning one item and not three (three children pages to the parent 'programme')? $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); Any thoughts? If I unpublish the last child, it returns the new 'last child' in programme. Weird?
a-ok Posted February 23, 2016 Author Posted February 23, 2016 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...
adrian Posted February 23, 2016 Posted February 23, 2016 I know I must be missing something obvious, but wasn't your first post getting you all items? If so, then why not just strip the beginning [ and ending ] and be done with it?
a-ok Posted February 23, 2016 Author Posted February 23, 2016 I know I must be missing something obvious, but wasn't your first post getting you all items? If so, then why not just strip the beginning [ and ending ] and be done with it? Yeah I tried that too, but it threw back a JS error saying unexpected token } $programme_json = substr($programme_json, 1, -1);
LostKobrakai Posted February 23, 2016 Posted February 23, 2016 I'm not sure if that's helpful, but json does only allow for either square-bracketed arrays or keyed objects (therefore the "0": {}). Also the api you linked does use [] brackets for features.
a-ok Posted February 25, 2016 Author Posted February 25, 2016 (edited) This is what I came 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); ?> Edited February 25, 2016 by LostKobrakai Fixed some indentations
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now