php - 在 PHP 中访问 JSON 数据中的值时遇到问题
问题描述
我必须尝试访问 PHP 中 JSON 数据中 4.9 的“文本”值。
$json = '{"destination_addresses" : [ "Ibadan-Abeokuta Rd, Mokola, Ibadan, Nigeria" ],
"origin_addresses" : [ "Ogunwale Crescent, Ibadan, Nigeria" ],
"rows" : [
{ "elements" : [
{ "distance" :
{ "text" : "4.9 mi", "value" : 7819 },
"duration" : { "text" : "18 mins", "value" : 1067 },
"status" : "OK" }
]
}
]
, "status" : "OK" }';
解决方案
您的 JSON 有点复杂。text
我在嵌套 JSON 的访问属性下面有一个解决方案。
$array = json_decode( $json, true );
echo ($array['rows'][0] ["elements"][0]['distance']['text']);
推荐阅读
- c# - 类方法不使用类变量
- reactjs - 在 MUI 主题中动态更改原色
- javascript - node.js 查询未完全执行并返回主块
- react-native - 未定义不是反应原生的对象(评估_this.ref.method)
- css - gatsby-image:如何将处理后的图像导入 css 并与 background-image 属性一起使用
- javascript - WordPress - 将所有媒体库文件移动到新文件夹(棘手)
- jmeter - 以分布式模式打开 JMeter
- javascript - 通过 jQuery 应用时出现“宽度”问题
- macos - Cocoa API 不断重置我们的 NSCursor
- sql - 使用带时区的时间戳提取日期