我在 php 中有这个 json 数组代码
$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);
?>
这给了我这样的数组值:
[{"id":18,"longitude":107.6123014,"latitude":-6.897495399999999,"name":"Kaos Oleh Oleh Khas Bandung & Kaos Otomotif"}]
我怎样才能像这样在纬度和经度上添加双引号:
[{"id":18,"longitude":"107.6123014","latitude":"-6.897495399999999","name":"Kaos Oleh Oleh Khas Bandung & Kaos Otomotif"}]
我试过用这段代码 explode
$results = explode('","', $results);
但它给了我警告:warning explode() expects parameter 2
在此先感谢您的帮助
最佳答案
转换为字符串
$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);
关于php - 用双引号 PHP 给出数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65109941/