3

i have this json array code in php


$url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-6.893238,107.604273&radius=100&type=point_of_interest&keyword=oleh-oleh&key=mykey";

$data = file_get_contents($url);
$json = json_decode($data);

$i = 0;
$results = array_map(function($result) use (&$i) {
    return array ("id" => ++$i , "longitude" => $result->geometry->location->lng, "latitude" => $result->geometry->location->lat, "description" => $result->vicinity, "name" => $result->name);
}, $json->results);

$results = explode('","', $results);

echo json_encode($results);
?>

Which gives me array values like this :

[{"id":18,"longitude":107.6123014,"latitude":-6.897495399999999,"name":"Kaos Oleh Oleh Khas Bandung & Kaos Otomotif"}]

How can I add double quotes on lat and long like this :

[{"id":18,"longitude":"107.6123014","latitude":"-6.897495399999999","name":"Kaos Oleh Oleh Khas Bandung & Kaos Otomotif"}]

i tried explode with this code

$results = explode('","', $results);

but it gives me warning : warning explode() expects parameter 2

Thanks in advance for the help

1 Answers1

1

cast to string

$results = array_map(function($result) use (&$i) {
    return array (
               "id" => ++$i , 
               "longitude" => (string) $result->geometry->location->lng, 
               "latitude" => (string) $result->geometry->location->lat,
               "description" => $result->vicinity, 
               "name" => $result->name
               );
}, $json->results);
Eden Moshe
  • 1,097
  • 1
  • 6
  • 16