使用PHP从JSON结果中获取特定项目

时间:2018-11-04 18:12:38

标签: php json

我需要有关JSON数据的帮助。我试图只获得一个特殊的值,而不确切知道哪个。

这是我的JSON数据:

            {
        "JourneyDetail":{
        "Stops":{
        "Stop":[{
        "name":"Essen Hbf",
        "id":"8000098",
        "lon":"7.014795",
        "lat":"51.451351",
        "routeIdx":"2",
        "arrTime":"10:38",
        "arrDate":"2018-02-22",
        "depTime":"10:41",
        "depDate":"2018-02-22",
        "track":"2"
        },{
        "name":"Duisburg Hbf",
        "id":"8000086",
        "lon":"6.775907",
        "lat":"51.429786",
        "routeIdx":"3",
        "arrTime":"10:52",
        "arrDate":"2018-02-22",
        "depTime":"10:55",
        "depDate":"2018-02-22",
        "track":"3"
        },{
        "name":"Düsseldorf Hbf",
        "id":"8000085",
        "lon":"6.794317",
        "lat":"51.219960",
        "routeIdx":"4",
        "arrTime":"11:06",
        "arrDate":"2018-02-22",
        "depTime":"11:08",
        "depDate":"2018-02-22",
        "track":"16"
        },{
        "name":"Köln Messe/Deutz Gl.11-12",
        "id":"8073368",
        "lon":"6.974065",
        "lat":"50.941717",
        "routeIdx":"5",
        "arrTime":"11:28",
        "arrDate":"2018-02-22",
        "depTime":"11:30",
        "depDate":"2018-02-22",
        "track":"11"
        },{
        "name":"Siegburg/Bonn",
        "id":"8005556",
        "lon":"7.203029",
        "lat":"50.793916",
        "routeIdx":"6",
        "arrTime":"11:42",
        "arrDate":"2018-02-22",
        "depTime":"11:43",
        "depDate":"2018-02-22",
        "track":"6"
        },{
        "name":"Montabaur",
        "id":"8000667",
        "lon":"7.825335",
        "lat":"50.444838",
        "routeIdx":"7",
        "arrTime":"12:02",
        "arrDate":"2018-02-22",
        "depTime":"12:03",
        "depDate":"2018-02-22",
        "track":"1"
        },{
        "name":"Limburg Süd",
        "id":"8003680",
        "lon":"8.096108",
        "lat":"50.382498",
        "routeIdx":"8",
        "arrTime":"12:13",
        "arrDate":"2018-02-22",
        "depTime":"12:14",
        "depDate":"2018-02-22",
        "track":"1"
        },{
        "name":"Frankfurt(M) Flughafen Fernbf",
        "id":"8070003",
        "lon":"8.570181",
        "lat":"50.053169",
        "routeIdx":"9",
        "arrTime":"12:33",
        "arrDate":"2018-02-22",
        "depTime":"12:36",
        "depDate":"2018-02-22",
        "track":"Fern 4"
        },{
        "name":"Frankfurt(Main)Hbf",
        "id":"8000105",
        "lon":"8.663785",
        "lat":"50.107149",
        "routeIdx":"10",
        "arrTime":"12:48",
        "arrDate":"2018-02-22",
        "depTime":"12:54",
        "depDate":"2018-02-22",
        "track":"7"
        },{
        "name":"Aschaffenburg Hbf",
        "id":"8000010",
        "lon":"9.143693",
        "lat":"49.980554",
        "routeIdx":"11",
        "arrTime":"13:22",
        "arrDate":"2018-02-22",
        "depTime":"13:23",
        "depDate":"2018-02-22",
        "track":"6"
        },{
        "name":"Würzburg Hbf",
        "id":"8000260",
        "lon":"9.935777",
        "lat":"49.801795",
        "routeIdx":"12",
        "arrTime":"14:07",
        "arrDate":"2018-02-22",
        "depTime":"14:10",
        "depDate":"2018-02-22",
        "track":"3"
        },{
        "name":"Nürnberg Hbf",
        "id":"8000284",
        "lon":"11.082989",
        "lat":"49.445615",
        "routeIdx":"13",
        "arrTime":"15:02",
        "arrDate":"2018-02-22",
        "depTime":"15:05",
        "depDate":"2018-02-22",
        "track":"9"
        },{
        "name":"München Hbf",
        "id":"8000261",
        "lon":"11.558339",
        "lat":"48.140229",
        "routeIdx":"14",
        "arrTime":"16:07",
        "arrDate":"2018-02-22",
        "track":"22"
        }]
        }
        }

这是我的PHP代码

$url = '....';
$content = file_get_contents($url);
$json = json_decode($content, true);

现在,我只想在该数组后面添加一个数组(名称):

    {
    "name":"Düsseldorf Hbf",
    "id":"8000085",
    "lon":"6.794317",
    "lat":"51.219960",
    "routeIdx":"4",
    "arrTime":"11:06",
    "arrDate":"2018-02-22",
    "depTime":"11:08",
    "depDate":"2018-02-22",
    "track":"16"
    },

有时,名称为“DüsseldorfHbf”的数组是第一个,有时是第三个……等等。这是幸运的。

有人有主意吗?

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您可以基于array_filtercurrent

使用此解决方案
$url = '....';
$content = file_get_contents($url);
$json = json_decode($content, true);

function hasName($stop) {
    return $stop['name'] == 'Düsseldorf Hbf';
}

$res = current(array_filter($json['JourneyDetail']['Stops']['Stop'], 'hasName'));

var_dump($res);

或者使用直接的,可能是更优化的解决方案:

$url = '....';
$content = file_get_contents($url);
$json = json_decode($content, true);

$res = getStopByName($json['JourneyDetail']['Stops']['Stop'], 'Düsseldorf Hbf');

var_dump($res);

function getStopByName($stops, $name) {
    foreach ($stops as $stop) {
        if ($stop['name'] == $name) {
            return $stop;
        }
    }
}

答案 1 :(得分:0)

获取某些特定对象时,请使用方括号。 echo $json[3];

尝试:

$content = file_get_contents($url);
$json = json_decode($content, true);
echo $json[3];