php - 用双引号 PHP 给出数组值

标签 php arrays json explode

我在 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/

相关文章:

android - 在 GSON 中反序列化动态数组

php - Laravel - 我正在尝试从数据库中删除一条记录返回 null(不工作)

javascript - 为什么我不能删除数组的元素?

javascript - 如何从 Javascript 中的多维数组中获取元素

Android:如何摆脱@Generated ("org.jsonschema2pojo")?

java - 如何使用 "?"no get Path Rest?

php - 如何在 PHP 中使用 POST 从 CKEDITOR 获取值?

php - 将 Facebook PHP SDK 与 Javascript SDK 结合使用

php - 无法获取表单数据以发布到 SQL(使用 MySQL 5.5.25)

c++ - 在 C++ 中声明和删除此版本的二维数组