我目前正在嘗試在變量中獲取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 ;
- 1 回答
- 0 關注
- 108 瀏覽
添加回答
舉報
0/150
提交
取消