为了账号安全,请及时绑定邮箱和手机立即绑定

PHP Curl Json:多个输出

PHP Curl Json:多个输出

PHP
函数式编程 2022-08-05 15:59:58
我目前正在尝试在变量中获取JSON解码输出的每个标题这就是对我有用的卷曲$curl = curl_init();curl_setopt_array($curl, array(    CURLOPT_URL => 'http://api.irail.be/disturbances/?format=json&lang=nl',    CURLOPT_RETURNTRANSFER => true,    CURLOPT_ENCODING => '',    CURLOPT_MAXREDIRS => 10,    CURLOPT_TIMEOUT => 30,    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,    CURLOPT_CUSTOMREQUEST => 'POST',    CURLOPT_HTTPHEADER => array(        'cache-control: no-cache',        'content-type: application/x-www-form-urlencoded'    ),));$response = curl_exec($curl);$err = curl_error($curl);curl_close($curl);// Decode JSON response and get only the data needed:$response = json_decode($response);$response = $response->disturbance[0];var_dump($response);$name = $response->title;echo $name;当我删除干扰背后的[0]时,我得到一个空白$name。有谁知道我该如何解决这个问题?谢谢
查看完整描述

1 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

您可以通过键访问对象,但“干扰”不是键,而是键可以取的值。type


首先需要筛选数据以仅获取 包含 的项目,然后才能获取标题。type = 'disturbance'


下面是一个示例:


$response = json_decode($response); 

$disturbanceItems = array_filter($response, function ($item){return $item->type == 'disturbance';}); 


echo $disturbanceItems[0]->title ;


查看完整回答
反对 回复 2022-08-05
  • 1 回答
  • 0 关注
  • 74 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信