2016-02-12 76 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'"; 
} 

希望有所帮助!