使用 php 循环 Curl 响应并提取信息
Looping through Curl response with php and pulling information out
我对编码还很陌生,正在研究 CURL 响应。我的任务是遍历结果并仅列出“ORGANISATION_NAME”。
我的努力是这样的,但显然失败了,我不确定为什么。
有人可以通过简单的解释指导我如何做到这一点吗?
foreach ($response[] as $key => $value) {
echo $value['ORGANISATION_NAME'];
}
这是数据。
[
{
"ORGANISATION_ID": 59931111,
"ORGANISATION_NAME": "Low Level Repository"
}, {
"ORGANISATION_ID": 59931112,
"ORGANISATION_NAME": "The Bike Shed"
}, {
"ORGANISATION_ID": 59931113,
"ORGANISATION_NAME": "Taxi's are us"
}, {
"ORGANISATION_ID": 59931114,
"ORGANISATION_NAME": "The apple tree"
}
]
好的,对于初学者来说,响应看起来像 JSON,因此您需要对其进行解码。
我也不太确定你的 foreach 循环发生了什么,试试这个:
foreach(json_decode($response) as $item){
echo $item->ORGANISATION_ID; // Returns the value for the object
echo $item->ORGANISATION_NAME;
}
如果您还没有准备好使用对象,您可以 return 和关联数组使用 json 通过将第二个参数设置为 true 进行解码:
foreach(json_decode($array, true) as $item){
echo $item['ORGANISATION_ID'];
echo $item['ORGANISATION_NAME'];
}
如果您计划将 PHP 个数组或对象发送到 JSON:json_encode();
我对编码还很陌生,正在研究 CURL 响应。我的任务是遍历结果并仅列出“ORGANISATION_NAME”。
我的努力是这样的,但显然失败了,我不确定为什么。
有人可以通过简单的解释指导我如何做到这一点吗?
foreach ($response[] as $key => $value) {
echo $value['ORGANISATION_NAME'];
}
这是数据。
[
{
"ORGANISATION_ID": 59931111,
"ORGANISATION_NAME": "Low Level Repository"
}, {
"ORGANISATION_ID": 59931112,
"ORGANISATION_NAME": "The Bike Shed"
}, {
"ORGANISATION_ID": 59931113,
"ORGANISATION_NAME": "Taxi's are us"
}, {
"ORGANISATION_ID": 59931114,
"ORGANISATION_NAME": "The apple tree"
}
]
好的,对于初学者来说,响应看起来像 JSON,因此您需要对其进行解码。
我也不太确定你的 foreach 循环发生了什么,试试这个:
foreach(json_decode($response) as $item){
echo $item->ORGANISATION_ID; // Returns the value for the object
echo $item->ORGANISATION_NAME;
}
如果您还没有准备好使用对象,您可以 return 和关联数组使用 json 通过将第二个参数设置为 true 进行解码:
foreach(json_decode($array, true) as $item){
echo $item['ORGANISATION_ID'];
echo $item['ORGANISATION_NAME'];
}
如果您计划将 PHP 个数组或对象发送到 JSON:json_encode();