2017-12-09 4 views
0

これは解析しようとしているJSONです。JSONを解析して表に表示する

{ 
    "kickers": [ 
    { 
     "_id": "iLntVcAmPn", 
     "nflPlayerName": "Stephen Gostkowski", 
     "nflPlayerNumber": 3, 
     "nflPlayerPosition": "K", 
     "nflPlayerTeam": "ne", 
     "nflPlayerCardType": "Common", 
     "nflPlayerNFLPlayerID": "00-0024333" 
    }, 
    { 
     "_id": "oLe3zNIpRH", 
     "nflPlayerName": "Justin Tucker", 
     "nflPlayerNumber": 9, 
     "nflPlayerPosition": "K", 
     "nflPlayerTeam": "bal", 
     "nflPlayerCardType": "Common", 
     "nflPlayerNFLPlayerID": "00-0029597" 
    } 
    ], 
    "quarterbacks": [ 
    { 
     "_id": "UXprgjbYGZ", 
     "nflPlayerName": "Carson Wentz", 
     "nflPlayerNumber": 11, 
     "nflPlayerPosition": "QB", 
     "nflPlayerTeam": "phi", 
     "nflPlayerCardType": "Common", 
     "nflPlayerNFLPlayerID": "00-0032950" 
    }, 
    { 
     "_id": "zZVjDrLQCs", 
     "nflPlayerName": "Aaron Rodgers", 
     "nflPlayerNumber": 12, 
     "nflPlayerPosition": "QB", 
     "nflPlayerTeam": "gb", 
     "nflPlayerCardType": "Common", 
     "nflPlayerNFLPlayerID": "00-0023459" 
    } 
    ], 
    "widereceivers": [ 
    { 
     "_id": "LoOT2JM8ot", 
     "nflPlayerName": "Emmanuel Sanders", 
     "nflPlayerNumber": 10, 
     "nflPlayerPosition": "WR", 
     "nflPlayerTeam": "den", 
     "nflPlayerCardType": "Common", 
     "nflPlayerNFLPlayerID": "00-0027685" 
    }, 
    { 
     "_id": "YnA6DkyZ48", 
     "nflPlayerName": "Brandin Cooks", 
     "nflPlayerNumber": 14, 
     "nflPlayerPosition": "WR", 
     "nflPlayerTeam": "ne", 
     "nflPlayerCardType": "Common", 
     "nflPlayerNFLPlayerID": "00-0031236" 
    } 
    ], 
    "tightends": [ 
    { 
     "_id": "mxrGujE01C", 
     "nflPlayerName": "Jordan Reed", 
     "nflPlayerNumber": 86, 
     "nflPlayerPosition": "TE", 
     "nflPlayerTeam": "was", 
     "nflPlayerCardType": "Common", 
     "nflPlayerNFLPlayerID": "00-0030472" 
    }, 
    { 
     "_id": "mxrGujE01C", 
     "nflPlayerName": "Jordan Reed", 
     "nflPlayerNumber": 86, 
     "nflPlayerPosition": "TE", 
     "nflPlayerTeam": "was", 
     "nflPlayerCardType": "Common", 
     "nflPlayerNFLPlayerID": "00-0030472" 
    } 
    ], 
    "runningbacks": [ 
    { 
     "_id": "u1uKHAt1n6", 
     "nflPlayerName": "Adrian Peterson", 
     "nflPlayerNumber": 28, 
     "nflPlayerPosition": "RB", 
     "nflPlayerTeam": "ne", 
     "nflPlayerCardType": "Common", 
     "nflPlayerNFLPlayerID": "00-0021306" 
    }, 
    { 
     "_id": "0AcCT71hRi", 
     "nflPlayerName": "Le'veon Bell", 
     "nflPlayerNumber": 26, 
     "nflPlayerPosition": "RB", 
     "nflPlayerTeam": "pit", 
     "nflPlayerCardType": "Common", 
     "nflPlayerNFLPlayerID": "00-0030496" 
    } 
    ], 
    "nfl_teams": [ 
    { 
     "_id": "ari", 
     "teamName": "Arizona Cardinals", 
     "teamCity": "Arizona", 
     "teamNameShort": "Cardinals", 
     "teamAbbreviated": "ARI", 
     "teamByeWeek": 8 
    }, 
    { 
     "_id": "bal", 
     "teamName": "Baltimore Ravens", 
     "teamCity": "Baltimore", 
     "teamNameShort": "Ravens", 
     "teamAbbreviated": "BAL", 
     "teamByeWeek": 10 
    } 
    ] 
} 

これは私が試してみました何....

<?php 

//Load the file 
$contents = file_get_contents('jsonfile.json'); 

//Decode the JSON data into a PHP array. 
$contentsDecoded = json_decode($contents); 

//print_r($contentsDecoded); 

foreach ($contentsDecoded as $key => $jsons) { 
    foreach($jsons as $key => $value) { 
     echo $value; 
    } 
} 
?> 

あなたが最初の表示を介してコンテンツを、私を歩くことができ、その後、私はそこから行くと、テーブル内のすべてを取得しようとすることができる場合であります。これは非常に簡単ですjavaですが、私はPHPでこれを行う前に試みたことがない。これは私にとって初めてのことです!

私が気にしない問題は、KICKERS配列を取得してオブジェクトを取得することです。私はいくつかの配列を持っています。

ご協力いただければ幸いです。

答えて

2

内部foreachループに同じ$key変数を使用することはできません。また、コード内の$valueはプレーヤーオブジェクトであり、直接エコーすることはできません。

ループ内でよりわかりやすい変数名を使用すると、参照するデータ構造のどの部分がわかるか分かります。例えば

foreach ($contentsDecoded as $position => $players) { 
    foreach($players as $player) { 
     echo $player->nflPlayerName; 
    } 
} 

それとも、具体的にちょうどキッカーたい場合:

foreach ($contentsDecoded->kickers as $kicker { 
    echo $kicker->nflPlayerName; 
} 
+0

甘いです。どうもありがとうございます!! – DroiDev

+0

あなたは大歓迎です。 –

関連する問題