2016-02-14 59 views
1

我試圖使用www.football-data.org獲得足球聯賽積分榜,但是我從未使用過JSON。從json獲取數據並僅使用一個結果作爲值

我目前使用下列內容:

$uri = 'http://api.football-data.org/v1/soccerseasons/398/leagueTable/'; 
$reqPrefs['http']['method'] = 'GET'; 
$reqPrefs['http']['header'] = 'X-Auth-Token:'.$api_key; 

$stream_context = stream_context_create($reqPrefs); 

$response = file_get_contents($uri, false, $stream_context); 
$details = json_decode($response); 

,試圖讓細節的一個團隊。目前,我正在使用:

echo "<pre>"; 
print_r($details); 
echo "</pre>"; 

得到以下顯示:

stdClass Object 
(
    [_links] => stdClass Object 
     (
      [self] => stdClass Object 
       (
        [href] => http://api.football-data.org/v1/soccerseasons/398/leagueTable/?matchday=26 
       ) 

      [soccerseason] => stdClass Object 
       (
        [href] => http://api.football-data.org/v1/soccerseasons/398 
       ) 

     ) 

    [leagueCaption] => Premier League 2015/16 
    [matchday] => 26 
    [standing] => Array 
     (
      [0] => stdClass Object 
       (
        [_links] => stdClass Object 
         (
          [team] => stdClass Object 
           (
            [href] => http://api.football-data.org/v1/teams/338 
           ) 

         ) 

        [position] => 1 
        [teamName] => Leicester City FC 
        [crestURI] => http://upload.wikimedia.org/wikipedia/en/6/63/Leicester02.png 
        [playedGames] => 25 
        [points] => 53 
        [goals] => 47 
        [goalsAgainst] => 27 
        [goalDifference] => 20 
        [wins] => 15 
        [draws] => 8 
        [losses] => 2 
        [home] => stdClass Object 
         (
          [goals] => 21 
          [goalsAgainst] => 13 
          [wins] => 7 
          [draws] => 4 
          [losses] => 1 
         ) 

        [away] => stdClass Object 
         (
          [goals] => 26 
          [goalsAgainst] => 14 
          [wins] => 8 
          [draws] => 4 
          [losses] => 1 
         ) 

       ) 

首先,我怎麼去獲得這些值,並將其保存在PHP可用的變量?

其次,是否有可能將這些數據縮小到一個團隊?例如說:

if($details->teamName == 'Manchester United') { $gamesPlayed = $details->gamesPlayed; } 

回答

2

你有其中有(其中包括)對象的數組命名爲standing和他們又將有teamName屬性的對象。所以,讓你會做

if($details->standing[0]->teamName == 'Manchester United') 

第一個或者你也可以遍歷standing,讓所有的人,如果萬一有多個條目

foreach($details->standing as $record) 
{ 
    if($record->teamName == 'Manchester United') 
     $gamesPlayed=$record->playedGames; 
} 
+0

嘛[0 ]與聯盟秩序有關,所以例如萊斯特目前是[0],但如果其他球隊超越他們,他們將是[0],所以我認爲將所有的數據,然後過濾它?但是我怎麼會在不知道自己的位置的情況下過濾出曼聯的統計數據呢? – BN83

+0

已更新爲 –

+0

完美謝謝。接受爲答案。 – BN83

相關問題