php - 如何在 laravel dd 函数中访问特定的数组值
问题描述
我只需要基于我在参数中输入的地址的地理位置坐标。我正在使用 curl 的请求。
这是我的卷曲请求
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=10%20BISHAN%STREET%2013&key='.$key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
dd($response);
我得到了回应
array:2 [▼
"results" => array:1 [▼
0 => array:6 [▼
"address_components" => array:6 [▶]
"formatted_address" => "10 Bishan Street 13, Singapore 579795"
"geometry" => array:3 [▼
"location" => array:2 [▼
"lat" => 1.349774
"lng" => 103.854975
]
"location_type" => "ROOFTOP"
"viewport" => array:2 [▶]
]
"place_id" => "ChIJR0tGixEX2jERVE4uZWfBN-o"
"plus_code" => array:2 [▶]
"types" => array:1 [▶]
]
]
"status" => "OK"
]
如果我做dd($response['result']);
我明白了
0 => array:6 [▼
"address_components" => array:6 [▶]
"formatted_address" => "10 Bishan Street 13, Singapore 579795"
"geometry" => array:3 [▼
"location" => array:2 [▼
"lat" => 1.349774
"lng" => 103.854975
]
"location_type" => "ROOFTOP"
"viewport" => array:2 [▼
"northeast" => array:2 [▼
"lat" => 1.3511229802915
"lng" => 103.85632398029
]
"southwest" => array:2 [▼
"lat" => 1.3484250197085
"lng" => 103.85362601971
]
]
]
"place_id" => "ChIJR0tGixEX2jERVE4uZWfBN-o"
"plus_code" => array:2 [▼
"compound_code" => "8VX3+WX Singapore"
"global_code" => "6PH58VX3+WX"
]
"types" => array:1 [▼
0 => "street_address"
]
]
]
有没有办法我可以location
直接使用dd
?感谢您的回答。
解决方案
如果您遵循层次结构,您只需在每个阶段添加相应的数组元素名称,这应该会给您...
$response['result'][0]['geometry']['location']
如果有更多结果,[0]
可能需要 a 。foreach
推荐阅读
- r - 我可以使用 R 将 Excel 工作簿保存为二进制文件吗?
- missing-data - MCMCglmm 中的错误:固定预测变量中的缺失值 - 但我没有缺失值
- javascript - Formik React Native 复选框
- ansible - Ansible playbook 将字节转换为 GB
- vim - 是否有一个 Vim 插件可以 TAB 完成 CTags 索引中的符号?
- python - 计算python pandas中的差异时值不正确
- flutter - “MqttMessage”类型的值不能分配给“MqttPublishMessage”类型的变量
- python - 斐波那契数列:寻找复合问题
- google-sheets - 如何在 Google 表格的 Query/ImportRange 函数的开头添加一列?
- python - 如何使用 .apply() 解决 pandas 广播错误?