如何使用PHP获取JSON的部分内容

时间:2018-03-07 23:02:35

标签: php json

从一个URL我得到一个JSON,我稍后会展示一段。我试图用一些PHP从这个JSON中提取信息。这就是我现在所拥有的:

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

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

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

JSON网址包含交通堵塞信息,以及路障和那种东西。对于我的网站,我试图回应所有的交通拥堵。为了试图回应一些信息,我试图回应发生果酱的第一条路,但我什么都没得到。我在网上找到的大部分例子比我在这里的例子简单,所以说实话,我不知道如何解决这个问题。

这是一段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": []
            }
        },

为什么我的echo无效?

1 个答案:

答案 0 :(得分:1)

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

的更改