从php多维数组中的json获取嵌套数据

时间:2018-12-24 07:53:55

标签: php arrays json

我有一个API,可以从NBA比赛中返回以下数据,这是单个游戏的JSON代码段。

 {
    "league": {
        "id": "4353138d-4c22-4396-95d8-5f587d2df25c",
        "name": "NBA",
        "alias": "NBA"
    },
    "season": {
        "id": "86a687a8-74fa-4c31-92a7-40498d3f3d8b",
        "year": 2018,
        "type": "PRE"
    },
    "games": [{
        "id": "feb73ae6-39e6-4613-94f3-37eca10515a0",
        "status": "closed",
        "coverage": "full",
        "scheduled": "2018-09-28T23:00:00+00:00",
        "home_points": 104,
        "away_points": 84,
        "track_on_court": true,
        "reference": "0011800001",
        "time_zones": {
            "venue": "US/Eastern",
            "home": "US/Eastern"
        },
        "venue": {
            "id": "b3dca541-859e-5301-bf90-4ec677a514a9",
            "name": "Wells Fargo Center",
            "capacity": 20478,
            "address": "3601 S. Broad Street",
            "city": "Philadelphia",
            "state": "PA",
            "zip": "19148",
            "country": "USA",
            "sr_id": "sr:venue:6068"
        },
        "broadcasts": [{
            "network": "NBCS-PH+",
            "type": "TV",
            "locale": "Home"
        }],
        "home": {
            "name": "Philadelphia 76ers",
            "alias": "PHI",
            "id": "583ec87d-fb46-11e1-82cb-f4ce4684ea4c",
            "sr_id": "sr:team:3420",
            "reference": "1610612755"
        },
        "away": {
            "name": "Melbourne United",
            "alias": "MEL",
            "id": "3bf4047b-ef03-42d4-848e-84bc7307958d"
        }
    },

现在我知道可以通过执行以下操作从games数组中获取所有详细信息:

我的密码

$data = json_decode(file_get_contents('http:test.com'), true);
$games_array = $data['games'];
foreach ($games_array as $game) {   
          echo $game['scheduled'];
    }

只需将索引插入games数组中,我就可以访问$game[]数组中的任何数据

上面代码的输出示例

2018-09-28T23:00:00+00:00
2018-09-28T23:30:00+00:00

我的问题

如何从venue数组以及homeaway数组访问数据/索引?如我所见,它们都嵌套在games数组中。任何帮助表示赞赏。

0 个答案:

没有答案