用双引号给PHP数组赋值

3

我在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"}]

我尝试使用此代码爆炸:

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

但是它给了我一个警告:warning explode() expects parameter 2

提前感谢您的帮助。


2
为什么需要引号? - u_mulder
6
将这些值在添加到数组的同时转换为字符串数据类型。 - CBroe
我认为你不需要将其转换为字符串,你的工具可以很好地处理数字值。 - u_mulder
@u_mulder 我尝试运行程序,但遗憾的是它没有显示数据。 - Squid Tenpoles
@CBroe 抱歉,我该怎么做? - Squid Tenpoles
显示剩余2条评论
1个回答

1
将其转换为字符串
$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);

或者您可以将引号与'"'+value+'"'连接起来。 - user4717133

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接