我目前正在尝试在一个变量中获取我的 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 回答
MMMHUHU
TA贡献1834条经验 获得超8个赞
您可以通过键访问您的对象,但“干扰”不是键,而是键可以采用的值type。
您需要首先过滤数据以仅获取带有 的项目type = 'disturbance',然后才能获取标题。
这是一个例子:
$response = json_decode($response);
$disturbanceItems = array_filter($response, function ($item){return $item->type == 'disturbance';});
echo $disturbanceItems[0]->title ;
- 1 回答
- 0 关注
- 93 浏览
添加回答
举报
0/150
提交
取消
