如何在页面中显示转换后的JSON PHP数组

时间:2014-03-25 00:07:23

标签: php arrays json

我正在连接到API,然后使用cURL将该数组转换为PHP数组(我认为)。我怎样才能把这个数组放到我的页面中让人们以更加令人愉悦的方式查看(比如变量)。

array(2) { ["summonerId"]=> int(19975245) ["games"]=> array(10) { [0]=> array(14) { ["gameId"]=> int(1321012446) ["invalid"]=> bool(false) ["gameMode"]=> string(7) "CLASSIC" ["gameType"]=> string(12) "MATCHED_GAME" ["subType"]=> string(15) "RANKED_SOLO_5x5" ["mapId"]=> int(1) ["teamId"]=> int(100) ["championId"]=> int(122) ["spell1"]=> int(14) ["spell2"]=> int(6) ["level"]=> int(30) ["createDate"]=> float(1395694236659) ["fellowPlayers"]=> array(9) { [0]=> array(3) { ["summonerId"]=> int(43168746) ["teamId"]=> int(100) ["championId"]=> int(60) } [1]=> array(3) { ["summonerId"]=> int(47321626) ["teamId"]=> int(200) ["championId"]=> int(121) } [2]=> array(3) { ["summonerId"]=> int(21498881) ["teamId"]=> int(200) ["championId"]=> int(77) } [3]=> array(3) { ["summonerId"]=> int(23237414) ["teamId"]=> int(200) ["championId"]=> int(79) } [4]=> array(3) { ["summonerId"]=> int(25290254) ["teamId"]=> int(100) ["championId"]=> int(412) } [5]=> array(3) { ["summonerId"]=> int(32603601) ["teamId"]=> int(100) ["championId"]=> int(51) } [6]=> array(3) { ["summonerId"]=> int(9969) ["teamId"]=> int(200) ["championId"]=> int(25) } [7]=> array(3) { ["summonerId"]=> int(263293) ["teamId"]=> int(200) ["championId"]=> int(236) } [8]=> array(3) { ["summonerId"]=> int(19712716) ["teamId"]=> int(100) ["championId"]=> int(103) } } ["stats"]=> array(40) { ["level"]=> int(15) ["goldEarned"]=> int(10708) ["numDeaths"]=> int(10) ["turretsKilled"]=> int(1) ["minionsKilled"]=> int(204) ["championsKilled"]=> int(6) ["goldSpent"]=> int(10915) ["totalDamageDealt"]=> int(131158) ["totalDamageTaken"]=> int(27011) ["killingSprees"]=> int(2) ["largestKillingSpree"]=> int(2) ["team"]=> int(100) ["win"]=> bool(false) ["neutralMinionsKilled"]=> int(1) ["largestMultiKill"]=> int(1) ["physicalDamageDealtPlayer"]=> int(118328) ["magicDamageDealtPlayer"]=> int(8277) ["physicalDamageTaken"]=> int(18495) ["magicDamageTaken"]=> int(7702) ["largestCriticalStrike"]=> int(548) ["timePlayed"]=> int(1764) ["totalHeal"]=> int(2608) ["totalUnitsHealed"]=> int(1) ["assists"]=> int(5) ["item0"]=> int(1055) ["item1"]=> int(3078) ["item2"]=> int(3134) ["item3"]=> int(3265) ["item4"]=> int(3035) ["item6"]=> int(3340) ["sightWardsBought"]=> int(2) ["magicDamageDealtToChampions"]=> int(2326) ["physicalDamageDealtToChampions"]=> int(15107) ["totalDamageDealtToChampions"]=> int(21586) ["trueDamageDealtPlayer"]=> int(4552) ["trueDamageDealtToChampions"]=> int(4152) ["trueDamageTaken"]=> int(814) ["wardPlaced"]=> int(9) ["neutralMinionsKilledEnemyJungle"]=> int(1) ["totalTimeCrowdControlDealt"]=> int(219) } } [1]=> array(14) { ["gameId"]=> int(1320945501) ["invalid"]=> bool(false)

并且这会使用不同的召唤者ID重复几次,而不是。

我用来从JSON转换为php的代码是:

<?php
$url="API_LINK_HERE";
//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);

// Will dump a beauty json :3
var_dump(json_decode($result, true));
?>

原始的JSON数组是:

{"summonerId":19975245,"games":[{"gameId":1320945501,"invalid":false,"gameMode":"CLASSIC","gameType":"MATCHED_GAME","subType":"RANKED_SOLO_5x5","mapId":1,"teamId":200,"championId":106,"spell1":14,"spell2":6,"level":30,"createDate":1395691918584,"fellowPlayers":[{"summonerId":22308749,"teamId":100,"championId":64},{"summonerId":19974069,"teamId":200,"championId":117},{"summonerId":43168746,"teamId":100,"championId":80},{"summonerId":22846462,"teamId":100,"championId":13},{"summonerId":32898052,"teamId":200,"championId":42},{"summonerId":40519573,"teamId":200,"championId":32},{"summonerId":29129833,"teamId":200,"championId":25},{"summonerId":22666731,"teamId":100,"championId":412},{"summonerId":165896,"teamId":100,"championId":51}],"stats":{"level":18,"goldEarned":13451,"numDeaths":6,"turretsKilled":1,"minionsKilled":208,"championsKilled":6,"goldSpent":12235,"totalDamageDealt":135938,"totalDamageTaken":43804,"doubleKills":1,"killingSprees":1,"largestKillingSpree":3,"team":200,"win":true,"neutralMinionsKilled":14,"largestMultiKill":2,"physicalDamageDealtPlayer":66392,"magicDamageDealtPlayer":65853,"physicalDamageTaken":30525,"magicDamageTaken":11895,"timePlayed":2098,"totalHeal":3932,"totalUnitsHealed":1,"assists":8,"item0":1055,"item1":1011,"item2":3065,"item3":3143,"item4":3068,"item5":3265,"item6":3340,"magicDamageDealtToChampions":7724,"physicalDamageDealtToChampions":8310,"totalDamageDealtToChampions":17663,"trueDamageDealtPlayer":3691,"trueDamageDealtToChampions":1629,"trueDamageTaken":1384,"wardPlaced":8,"neutralMinionsKilledEnemyJungle":2,"neutralMinionsKilledYourJungle":12,"totalTimeCrowdControlDealt":1838}},

1 个答案:

答案 0 :(得分:1)

这里有一些PHP代码可以帮助您入门。对于所有ID,您可能希望从游戏的API中提取更多数据,以便您可以为ID添加名称/友好标识符。

您提供的JSON无效,因此我为您修复了它:)。

//JSON object obtained from the cURL call.
$json = '
{
    "summonerId": 19975245,
    "games": [
        {
            "gameId": 1320945501,
            "invalid": false,
            "gameMode": "CLASSIC",
            "gameType": "MATCHED_GAME",
            "subType": "RANKED_SOLO_5x5",
            "mapId": 1,
            "teamId": 200,
            "championId": 106,
            "spell1": 14,
            "spell2": 6,
            "level": 30,
            "createDate": 1395691918584,
            "fellowPlayers": [
                {
                    "summonerId": 22308749,
                    "teamId": 100,
                    "championId": 64
                },
                {
                    "summonerId": 19974069,
                    "teamId": 200,
                    "championId": 117
                },
                {
                    "summonerId": 43168746,
                    "teamId": 100,
                    "championId": 80
                },
                {
                    "summonerId": 22846462,
                    "teamId": 100,
                    "championId": 13
                },
                {
                    "summonerId": 32898052,
                    "teamId": 200,
                    "championId": 42
                },
                {
                    "summonerId": 40519573,
                    "teamId": 200,
                    "championId": 32
                },
                {
                    "summonerId": 29129833,
                    "teamId": 200,
                    "championId": 25
                },
                {
                    "summonerId": 22666731,
                    "teamId": 100,
                    "championId": 412
                },
                {
                    "summonerId": 165896,
                    "teamId": 100,
                    "championId": 51
                }
            ],
            "stats": {
                "level": 18,
                "goldEarned": 13451,
                "numDeaths": 6,
                "turretsKilled": 1,
                "minionsKilled": 208,
                "championsKilled": 6,
                "goldSpent": 12235,
                "totalDamageDealt": 135938,
                "totalDamageTaken": 43804,
                "doubleKills": 1,
                "killingSprees": 1,
                "largestKillingSpree": 3,
                "team": 200,
                "win": true,
                "neutralMinionsKilled": 14,
                "largestMultiKill": 2,
                "physicalDamageDealtPlayer": 66392,
                "magicDamageDealtPlayer": 65853,
                "physicalDamageTaken": 30525,
                "magicDamageTaken": 11895,
                "timePlayed": 2098,
                "totalHeal": 3932,
                "totalUnitsHealed": 1,
                "assists": 8,
                "item0": 1055,
                "item1": 1011,
                "item2": 3065,
                "item3": 3143,
                "item4": 3068,
                "item5": 3265,
                "item6": 3340,
                "magicDamageDealtToChampions": 7724,
                "physicalDamageDealtToChampions": 8310,
                "totalDamageDealtToChampions": 17663,
                "trueDamageDealtPlayer": 3691,
                "trueDamageDealtToChampions": 1629,
                "trueDamageTaken": 1384,
                "wardPlaced": 8,
                "neutralMinionsKilledEnemyJungle": 2,
                "neutralMinionsKilledYourJungle": 12,
                "totalTimeCrowdControlDealt": 1838
            }
        }
    ]
}
';
//convert the JSON object to a PHP associative array 
$array = json_decode($json, true);

echo 'Summoner: ' . $array['summonerId'] . '<br />';
//this is a counter for the loop, so we can say Game 1, Game 2, etc...
$i = 1;
//loop through all the games
foreach($array['games'] as $game)
{
    echo '<h1>Game ' . $i . '</h1>';

    echo 'Mode: ' . $game['gameMode'] . '<br />';

    echo 'Type: ' . $game['gameType'] . '<br />';

    echo 'Subtype: ' . $game['subType'] . '<br />';

    echo 'Map: ' . $game['mapId'] . '<br />';

    echo 'Champion: ' . $game['championId'] . '<br />'; 

    echo 'Fellow Players <br /><br />';
    //under each game there is an array of all the players in the game, so let's loop over the players
    foreach($game['fellowPlayers'] as $player)
    {
        echo 'Player ID: ' . $player['summonerId'] . '<br />';
        echo 'Team: ' . $player['teamId'] . '<br /><br />';
    }

    echo '<hr />';

    $i++;
}