从外部URL从多个JSON嵌套获取值

时间:2018-07-25 20:49:46

标签: php json multidimensional-array

我无法从外部URL显示JSON中的值。以下是我从原始工作项目转换而来的内容,该项目具有不同的嵌套格式。如何为每个循环获取Rank,FirstName和Passing Yards中的值?我已经阅读了有关stackoverflow的其他文章,包括this one,但仍然无法弄清楚。

<?
$url = 'MY URL.com'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data); // decode the JSON feed

//Traverse array and get the data
foreach($characters as $mydata) {

echo $mydata->Name . "<br>";
                 foreach($mydata->Subcategories as $key => $value)
    {
        ?>
<tr>
<td><? echo $value->Leaders->Rank ; ?></td>
<td><? echo $value->Leaders->Rank->Athlete->FirstName . "<br>"; ?></td>
</tr>
 <?
    }                                                                                             
} ?>

并且来自外部URL的实际数据(简称为前两个)是:

  {
  "TeamStats": false,
  "Minimum": "",
  "Categories": [
    {
      "Name": "Offense",
      "Subcategories": [
        {
          "Name": "Passing",
          "Leaders": [
            {
              "Rank": "1",
              "School": "Test School 1",
              "TeamStats": false,
              "EventName": "",
              "EventCity": "",
              "EventState": "",
              "Athlete": [
                {
                  "ID": "84dc4b85bffd",
                  "FirstName": "John",
                  "LastName": "Doe",
                  "ClassYear": "Junior",
                  "Position": "QB"
                }
              ],
              "Stats": [
                {
                  "Header": "Yds",
                  "Name": "PassingYards",
                  "Value": "3884"
                },
                {
                  "Header": "Y/G",
                  "Name": "PassingYardsPerGame",
                  "Value": "270.3"
                },
                {
                  "Header": "Cmp",
                  "Name": "PassingComp",
                  "Value": "247"
                },
                {
                  "Header": "Att",
                  "Name": "PassingAtt",
                  "Value": "397"
                },
                {
                  "Header": "Pct",
                  "Name": "CompletionPercentage",
                  "Value": ".622"
                },
                {
                  "Header": "TD Passes",
                  "Name": "PassingTD",
                  "Value": "38"
                },
                {
                  "Header": "Int",
                  "Name": "PassingInt",
                  "Value": "14"
                },
                {
                  "Header": "Pct",
                  "Name": "QbRating",
                  "Value": "110.9"
                },
                {
                  "Header": "GP",
                  "Name": "GamesPlayed",
                  "Value": "14"
                }
              ]
            },
            {
              "Rank": "2",
              "School": "School 2",
              "TeamStats": false,
              "EventName": "",
              "EventCity": "",
              "EventState": "",
              "Athlete": [
                {
                  "ID": "45d6",
                  "FirstName": "Jack",
                  "LastName": "Smith",
                  "ClassYear": "Sophomore",
                  "Position": "QB"
                }
              ],
              "Stats": [
                {
                  "Header": "Yds",
                  "Name": "PassingYards",
                  "Value": "3543"
                },
                {
                  "Header": "Y/G",
                  "Name": "PassingYardsPerGame",
                  "Value": "255.5"
                },
                {
                  "Header": "Cmp",
                  "Name": "PassingComp",
                  "Value": "218"
                },
                {
                  "Header": "Att",
                  "Name": "PassingAtt",
                  "Value": "374"
                },
                {
                  "Header": "Pct",
                  "Name": "CompletionPercentage",
                  "Value": ".583"
                },
                {
                  "Header": "TD Passes",
                  "Name": "PassingTD",
                  "Value": "46"
                },
                {
                  "Header": "Int",
                  "Name": "PassingInt",
                  "Value": "11"
                },
                {
                  "Header": "Pct",
                  "Name": "QbRating",
                  "Value": "117.8"
                },
                {
                  "Header": "GP",
                  "Name": "GamesPlayed",
                  "Value": "14"
                }
              ]
            },

感谢您提供有关如何深入到Rank,FirstName和PassingYards值的指导。

0 个答案:

没有答案