gpt4 book ai didi

php - 如何将数组结果转换为字符串并将其回显

转载 作者:行者123 更新时间:2023-12-04 10:46:31 24 4
gpt4 key购买 nike

如果这真的很明显,我很抱歉我真的很难让它工作。

我按如下方式调用网址:

$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$postcode1&destinations=$postcode2&mode=driving&language=en-EN&sensor=false&key=MY-KEY";

我有以下返回数组:
Array
(
[destination_addresses] => Array
(
[0] => Addres_1
)

[origin_addresses] => Array
(
[0] => Address_2
)

[rows] => Array
(
[0] => Array
(
[elements] => Array
(
[0] => Array
(
[distance] => Array
(
[text] => 18.9 km
[value] => 18862
)

[duration] => Array
(
[text] => 20 mins
[value] => 1184
)

[status] => OK
)

)

)

)

[status] => OK
)

我正在尝试按如下方式获取字符串中的距离并将其回显
$data = file_get_contents($url);
$result = json_decode($data, true);
$distance = $result['rows'][0]['elements'][0]['distance']['text']['value'];

echo $distance此 echo 结束 1它应该在哪里 18862
欢迎任何帮助

最佳答案

只需删除 ['text']来自:

$distance = $result['rows'][0]['elements'][0]['distance']['text']['value'];

进行中:
$distance = $result['rows'][0]['elements'][0]['distance']['value'];
text不是 value 的父级

关于php - 如何将数组结果转换为字符串并将其回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59680805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com