2017-05-24 64 views
0

我有這樣的數據:http://api.tvmaze.com/shows?page=0 我想實現的是循環數據並回顯每個ID。這是我使用的代碼:在數組中循環陣列

<?php 

$url = ('http://api.tvmaze.com/shows?page=0'); 
$json = file_get_contents($url); 

$response = json_decode($json, TRUE); 

foreach($response as $serie){ 

    foreach($serie as $info => $value){ 
    echo $info->$value["id"]; 
    } 

} 

我真的不知道我在做什麼錯誤..你們有什麼想法嗎?

Greetz,

+0

什麼是你在'$ response'陣列? – Virb

+0

print_r($ value)並檢查您是否獲取數據? – rahulsm

回答

1

請嘗試下面的代碼獲取id和url。您必須使用數組,因爲您在json_decode()中傳遞TRUE

<?php 

$url = ('http://api.tvmaze.com/shows?page=0'); 
$json = file_get_contents($url); 

$response = json_decode($json, TRUE); 

foreach($response as $serie) 
{ 
    echo $serie['id']."->".$serie['url']."<br>"; 
} 
?> 
1

保持簡單獲取你想要的ID,

$url = ('http://api.tvmaze.com/shows?page=0'); 
$json = file_get_contents($url); 
$response = json_decode($json, TRUE); 
echo "<pre>"; 

foreach ($response as $value) { 
    echo $value['id']."<br/>"; // you will get ids in here only 
}