从php中的多维json获取信息

时间:2014-03-02 16:53:45

标签: php arrays json multidimensional-array

我试图从多维json数组中获取信息,但似乎无法正确使用。

以下是您可以在http://109.255.189.130:3000/

看到的网址输出示例
{ "vnc_version": "2.1.0.0", "mod_version": "1.0.0.0", "server": { "name": "Pure Blood", "framework": "Microsoft Windows NT 6.2.9200.0" }, "stats": { "uptime": 53462.0, "uptime_peak": 53462.0, "online": 1, "online_max": 1, "online_peak": 2, "unique": 1, "unique_max": 1, "unique_peak": 2, "items": 111752, "items_max": 112259, "items_peak": 112259, "mobiles": 37963, "mobiles_max": 37976, "mobiles_peak": 37978, "guilds": 0, "guilds_max": null, "guilds_peak": 0 }, "players": [ { "info": { "id": 1, "name": "aN.Droid", "title": "", "profile": "", "guild_id": -1, "guild_abbr": "" }, "stats": [ ], "skills": [ ], "equip": [ ] } ], "guilds": [ ] }

我想做的是回应播放器阵列中的名字。将有不止一个球员。

任何人都可以帮助我,并指出我正确的方向来获取这些信息吗?

我对json很新,所以请原谅我对这个问题的无知。

谢谢!

2 个答案:

答案 0 :(得分:0)

$json = '{ "vnc_version": "2.1.0.0", "mod_version": "1.0.0.0", "server": { "name": "Pure Blood", "framework": "Microsoft Windows NT 6.2.9200.0" }, "stats": { "uptime": 53462.0, "uptime_peak": 53462.0, "online": 1, "online_max": 1, "online_peak": 2, "unique": 1, "unique_max": 1, "unique_peak": 2, "items": 111752, "items_max": 112259, "items_peak": 112259, "mobiles": 37963, "mobiles_max": 37976, "mobiles_peak": 37978, "guilds": 0, "guilds_max": null, "guilds_peak": 0 }, "players": [ { "info": { "id": 1, "name": "aN.Droid", "title": "", "profile": "", "guild_id": -1, "guild_abbr": "" }, "stats": [ ], "skills": [ ], "equip": [ ] } ], "guilds": [ ] }';
$arr = json_decode($json, true);
foreach($arr["players"] as $player) print($player["info"]["name"]."<br/>");

答案 1 :(得分:0)

<?php

$json = '{ "vnc_version": "2.1.0.0", "mod_version": "1.0.0.0", "server": { "name": "Pure Blood", "framework": "Microsoft Windows NT 6.2.9200.0" }, "stats": { "uptime": 54383.3, "uptime_peak": 54383.3, "online": 1, "online_max": 1, "online_peak": 2, "unique": 1, "unique_max": 1, "unique_peak": 2, "items": 111672, "items_max": 112259, "items_peak": 112259, "mobiles": 37944, "mobiles_max": 37976, "mobiles_peak": 37978, "guilds": 0, "guilds_max": null, "guilds_peak": 0 }, "players": [ { "info": { "id": 1, "name": "aN.Droid", "title": "", "profile": "", "guild_id": -1, "guild_abbr": "" }, "stats": [ ], "skills": [ ], "equip": [ ] } ], "guilds": [ ] }';
$array = json_decode($json, true);
// you want 'true' as the second parameter so it tunrs this JSON data into a multidimensional array instead of objects.

foreach($array['players'] as $player){
    print_r($player['info']);
    //etc.. do what you want with each player
}