Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过JSON解析并显示在表中_Php_Json - Fatal编程技术网

Php 通过JSON解析并显示在表中

Php 通过JSON解析并显示在表中,php,json,Php,Json,这就是我试图解析的JSON { "kickers": [ { "_id": "iLntVcAmPn", "nflPlayerName": "Stephen Gostkowski", "nflPlayerNumber": 3, "nflPlayerPosition": "K", "nflPlayerTeam": "ne", "nflPlayerCardType": "Common", "nflPlaye

这就是我试图解析的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中这样做。这对我来说是新的

我没有得到的问题是我得到了踢球者数组,然后得到了里面的对象。但是我有一些数组


非常感谢您的帮助。

您不能在内部
foreach
循环中使用相同的
$key
变量。另外,代码中的
$value
是一个播放器对象,不能直接回显

如果在循环中使用更具描述性的变量名,这样就可以知道它们引用的是数据结构的哪一部分,这将非常有用

例如:

foreach ($contentsDecoded as $position => $players) { 
     foreach($players as $player) {
         echo $player->nflPlayerName; 
    }
}
或者,如果你特别想要踢者:

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

在内部
foreach
循环中不能使用相同的
$key
变量。另外,代码中的
$value
是一个播放器对象,不能直接回显

如果在循环中使用更具描述性的变量名,这样就可以知道它们引用的是数据结构的哪一部分,这将非常有用

例如:

foreach ($contentsDecoded as $position => $players) { 
     foreach($players as $player) {
         echo $player->nflPlayerName; 
    }
}
或者,如果你特别想要踢者:

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

好极了。非常感谢你!!好极了。非常感谢你!!