I want to use a Wordpress backend with advanced custom fields and the json Wordpress API to serve data to a mobile app.
For this project the client needs to select a location in the maps custom field. The API should return a json array with some other data and the data from the Google maps field.
The object the API returns is formatted as follows:
{
"0": [
{
"title": "Test 1",
"evenement_afbeelding": "38",
"kunstenaars": "Name One",
"openingstijden": "9:00 - 18:30",
"evenementbeschrijving": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ac pharetra tortor.",
"evenement_lokatie": "a:3:{s:7:\"address\";s:40:\"Jansweg 50, 2011 KN Haarlem, Netherlands\";s:3:\"lat\";s:17:\"52.38513460768028\";s:3:\"lng\";s:17:\"4.638633728027344\";}",
"biografie": "<strong>Lorem ipsum dolor sit amet</strong>\r\n\r\nConsectetur adipiscing elit. Nullam ac pharetra tortor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos."
}
],
"status": "ok"
}
The weird thing is that the value on key "evenement_lokatie" (the Google maps field) returns a string that I cannot convert to an array. The value on the "evenement_lokatie" key should contain another array like this:
{
"0": [
{
"title": "Test 1",
"evenement_afbeelding": "38",
"kunstenaars": "Name One",
"openingstijden": "9:00 - 18:30",
"evenementbeschrijving": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ac pharetra tortor.",
"evenement_lokatie": [
{
"Stad": "Haarlem",
"lang": "32143241",
"lat": "721321"
}
],
"biografie": "<strong>Lorem ipsum dolor sit amet</strong>\r\n\r\nConsectetur adipiscing elit. Nullam ac pharetra tortor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos."
}
],
"status": "ok"
}
This is the PHP code that should format the data in wordpress:
<?php
class json_api_events_controller{
public function hello_world(){
return array('message'=>'Hello World!');
}
public function get_events(){
$array = array();
$args = array('post_type'=>'evenementen', 'posts_per_page'=>-1, 'order_by'=>'title', 'order'=>'ASC');
$loop = new WP_Query($args);
$counter = 0;
while ( $loop->have_posts() ) : $loop->the_post();
$id = get_the_id();
$custom = get_post_custom($id);
// add the title
$array[$id][$counter]['title'] = get_the_title();
// add all the custom fields
foreach($custom as $k => $v){
// if key starts with '_' symbol, don't add to the array
if(strpos($k, '_') !== 0) {
$array[$id][$counter][$k] = array_shift($v);
}
}
$counter++;
endwhile;
return $array;
}
}
Also there are weird chracters in the string, like ";s:17:" and ";s:40:". Can anyone show me how I can convert such a string to an array like I described?
Thanks in advance!