Reputation: 3044
This is the JSON that I am trying to parse through.
{
"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
}
]
}
This is what I have tried....
<?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;
}
}
?>
If you can walk me through display the content first, and then I can go from there and try to get everything in a table. This is very easy is java, but I have never tried to do this in php before. This is new to me!
The issue I dont get is I get to get the KICKERS array, and then get the objects inside. but I have a few arrays.
Any help would be very appreciated.
Upvotes: 0
Views: 31
Reputation: 41820
You cannot use the same $key
variable in your inner foreach
loop. Also, $value
in your code is a player object, which cannot be echoed directly.
It will be helpful if you use more descriptive variable names in your loops so you can tell which part of the data structure they are referencing.
For example:
foreach ($contentsDecoded as $position => $players) {
foreach($players as $player) {
echo $player->nflPlayerName;
}
}
Or if you specifically want just the kickers:
foreach ($contentsDecoded->kickers as $kicker {
echo $kicker->nflPlayerName;
}
Upvotes: 2