2016-02-12 75 views
0

我使用API​​比賽服務,但我不知道我應該如何從我的json文本到PHP變量的數據。使用來自JSON文件的API數據與PHP

這是PHP代碼,使JSON文本:

$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); 
$header   = substr($output, 0, $header_size); 
$body   = json_decode(substr($output, $header_size)); 

這裏是我的JSON文本:

array(8) 
{ 
    [0]=> object(stdClass)#1 (12) {  
     ["id"]=> string(24) "5666e0ef150ba051458b456b" 
     ["discipline"]=> string(16) "counterstrike_go" 
     ["name"]=> string(18) "A csgo tournament #1" 
     ["full_name"]=> string(18) "A csgo tournament #1" 
     ["status"]=> string(9) "completed" 
     ["date_start"]=> string(10) "2015-12-12" 
     ["date_end"]=> string(10) "2015-12-13" 
     ["online"]=> bool(false) 
     ["location"]=> string(19) "Online" 
     ["country"]=> string(2) "SE" 
     ["size"]=> int(32) 
     ["archived"]=> bool(false) 
    } 
    [1]=> object(stdClass)#2 (12) { 
     ["id"]=> string(24) "5652cc7c150ba0e3568b4567" 
     ["discipline"]=> string(15) "leagueoflegends" 
     ["name"]=> string(16) "A csgo tournament #1" 
     ["full_name"]=> NULL 
     ["status"]=> string(5) "setup" 
     ["date_start"]=> string(10) "2015-12-11" 
     ["date_end"]=> string(10) "2015-12-12" 
     ["online"]=> bool(false) 
     ["location"]=> string(8) "Online" 
     ["country"]=> string(2) "SE" 
     ["size"]=> int(16) 
     ["archived"]=> bool(false) 
    } 
    [2]=> object(stdClass)#3 (12) { 
     ["id"]=> string(24) "555dd064140ba040588b456e" 
     ["discipline"]=> string(16) "counterstrike_go" 
     ["name"]=> string(30) "A csgo tournament #1" 
     ["full_name"]=> string(30) "A csgo tournament #1" 
     ["status"]=> string(7) "running" 
     ["date_start"]=> string(10) "2015-06-05" 
     ["date_end"]=> string(10) "2015-06-05" 
     ["online"]=> bool(true) 
     ["location"]=> string(12) "#Online" 
     ["country"]=> string(2) "SE" 
     ["size"]=> int(128) 
     ["archived"]=> bool(false) 
    } 
} 
+0

假設數組是'$ body',你應該可以通過'$ body [0] - >「id」'來訪問數據。 –

+0

如果我想將它們循環出來,我該怎麼辦?所以我可以得到每場比賽。 – Quarskel

回答

0

中循環,並打印每一場比賽做類似

for ($i = 0; $i < count($body); $i++) { 
    echo "$body[0]->'name'"; 
} 

希望有所幫助!