dongpo5239 2018-03-07 23:02
浏览 51

如何用PHP获取JSON的部分[重复]

This question already has an answer here:

From a a URL I am getting a JSON which I will show a piece of later. I am trying to extract information from this JSON with some PHP. This is what I have until now:

$url = 'https://www.anwb.nl/feeds/gethf';

$result = file_get_contents($url);
$array = json_decode($result, TRUE);

echo $array[1]['roadEntries'][0]['road'];

The JSON url contains traffic jam information, as well as road blocks and that sort of stuff. For my website I am trying to echo all the traffic jams. To kind of try to echo some information I tried to echo the first road where the jam is taking place, but I am getting nothing. Most of the examples I found online were way simpler than what I have here, so to be honest, I have no idea how to tackle this.

Here is a piece of de JSON:

{
    "dateTime": "20180307, 23:35",
    "roadEntries": [
        {
            "road": "A1",
            "roadType": "aWegen",
            "events": {
                "trafficJams": [],
                "roadWorks": [
                    {
                        "msgNr": "114452273",
                        "from": "afrit Soest",
                        "fromLoc": {
                            "lat": 52.22478,
                            "lon": 5.25528
                        },
                        "to": "afrit Soest",
                        "toLoc": {
                            "lat": 52.22478,
                            "lon": 5.25528
                        },
                        "location": "A1 Amsterdam richting Amersfoort",
                        "segStart": "Amsterdam",
                        "segEnd": "Amersfoort",
                        "start": "2018-03-07T19:00:00",
                        "startDate": "7 March 2018 20:00 uur",
                        "stop": "2018-03-08T04:30:00",
                        "stopDate": "8 March 2018 05:30 uur",
                        "reason": "De op- en afrit is dicht. Van 7 March 2018 20:00 uur tot 8 March 2018 05:30 uur.",
                        "description": "Bij afrit Soest. De op- en afrit is dicht. Van 7 March 2018 20:00 uur tot 8 March 2018 05:30 uur.",
                        "events": [
                            {
                                "alertC": "408",
                                "text": "De op- en afrit is dicht"
                            }
                        ]
                    },
                    {
                        "msgNr": "114594347",
                        "from": "Naarden",
                        "fromLoc": {
                            "lat": 52.28153,
                            "lon": 5.18393
                        },
                        "to": "Naarden",
                        "toLoc": {
                            "lat": 52.28153,
                            "lon": 5.18393
                        },
                        "location": "A1 Amsterdam richting Amersfoort",
                        "segStart": "Amsterdam",
                        "segEnd": "Amersfoort",
                        "start": "2018-03-07T23:00:00",
                        "startDate": "8 March 2018 00:00 uur",
                        "stop": "2018-03-08T04:00:00",
                        "stopDate": "8 March 2018 05:00 uur",
                        "reason": "De afrit is dicht. Van 8 March 2018 00:00 uur tot 8 March 2018 05:00 uur.",
                        "description": "Bij Naarden. De afrit is dicht. Van 8 March 2018 00:00 uur tot 8 March 2018 05:00 uur.",
                        "events": [
                            {
                                "alertC": "407",
                                "text": "De afrit is dicht"
                            }
                        ]
                    }
                ],
                "radars": []
            }
        },
        {
            "road": "A2",
            "roadType": "aWegen",
            "events": {
                "trafficJams": [],
                "roadWorks": [
                    {
                        "msgNr": "114452349",
                        "from": "afrit Kerkdriel",
                        "fromLoc": {
                            "lat": 51.75815,
                            "lon": 5.28296
                        },
                        "to": "afrit Kerkdriel",
                        "toLoc": {
                            "lat": 51.75815,
                            "lon": 5.28296
                        },
                        "location": "A2 Utrecht richting 's-Hertogenbosch",
                        "segStart": "Utrecht",
                        "segEnd": "'s-Hertogenbosch",
                        "start": "2018-03-07T21:30:00",
                        "startDate": "7 March 2018 22:30 uur",
                        "stop": "2018-03-09T05:00:00",
                        "stopDate": "9 March 2018 06:00 uur",
                        "reason": "De oprit is dicht. Van 7 March 2018 22:30 uur tot 9 March 2018 06:00 uur.",
                        "description": "Bij afrit Kerkdriel. De oprit is dicht. Van 7 March 2018 22:30 uur tot 9 March 2018 06:00 uur.",
                        "events": [
                            {
                                "alertC": "406",
                                "text": "De oprit is dicht"
                            }
                        ]
                    }
                ],
                "radars": []
            }
        },
        {
            "road": "A4",
            "roadType": "aWegen",
            "events": {
                "trafficJams": [
                    {
                        "msgNr": "114692360",
                        "from": "Leidschendam",
                        "fromLoc": {
                            "lat": 52.07356,
                            "lon": 4.39532
                        },
                        "to": "Zoeterwoude-Dorp",
                        "toLoc": {
                            "lat": 52.12969,
                            "lon": 4.49096
                        },
                        "location": "A4 Den Haag richting Amsterdam",
                        "segStart": "Den Haag",
                        "segEnd": "Amsterdam",
                        "start": "2018-03-07T20:01:00",
                        "startDate": "7 March 2018 21:01 uur",
                        "delay": 480,
                        "distance": 2000,
                        "reason": "Wegwerkzaamheden. Twee rijstroken zijn dicht.",
                        "description": "Tussen Leidschendam en Zoeterwoude-Dorp. Wegwerkzaamheden. Twee rijstroken zijn dicht.",
                        "events": [
                            {
                                "alertC": "701",
                                "text": "Wegwerkzaamheden"
                            },
                            {
                                "alertC": "101",
                                "text": "Stilstaand verkeer"
                            },
                            {
                                "alertC": "505",
                                "text": "Twee rijstroken zijn dicht"
                            }
                        ]
                    }
                ],
                "roadWorks": [],
                "radars": []
            }
        },

Why is my echo not working?

</div>
  • 写回答

1条回答 默认 最新

  • dousao8152 2018-03-07 23:46
    关注

    Change for $array['roadEntries'][0]['road'];

    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上