将数组中的大量数据放入可用数据中?

时间:2014-01-28 21:44:54

标签: php arrays api

好的,大家好,

我今天一直在搞乱我的小方案(对我坚持的事情有一些很大的帮助,而且我有一个新的小问题,我似乎无法理解。我确信它只是我愚蠢。

所以基本上,我有很多来自游戏(LoL)的数据但是我不知道如何构造它以便它是可读的,我是否必须将它放入数组并爆炸内容?如果是这样的话?

这就是我从API中提取数据的原因:

<?php
                $summoner_name = 'amphios';
                $summoner_id = 21554735;

                $profile = new riotapi('euw');

                $summonername = $profile->getSummonerByName($summoner_name);
                print_r($summonername);

                $summonerstats = $profile->getStats($summoner_id, 'ranked');
                print_r($summonerstats);

                $getsummoner = $profile->getSummoner($summoner_id, 'name');
                print_r($getsummoner);

            ?>

这就是它所显示的内容(抱歉文字的大墙):

{"id":21554735,"name":"Amphios","profileIconId":588,"summonerLevel":30,"revisionDate":1390725829000}{"summonerId":21554735,"modifyDate":1390725829000,"champions":[{"id":89,"name":"Leona","stats":{"totalSessionsPlayed":1,"totalSessionsLost":1,"totalSessionsWon":0,"totalChampionKills":0,"totalDamageDealt":27547,"totalDamageTaken":21722,"mostChampionKillsPerSession":0,"totalMinionKills":17,"totalDoubleKills":0,"totalTripleKills":0,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":6,"totalGoldEarned":7288,"mostSpellsCast":0,"totalTurretsKilled":0,"totalPhysicalDamageDealt":6559,"totalMagicDamageDealt":20987,"totalFirstBlood":0,"totalAssists":12,"maxChampionsKilled":0,"maxNumDeaths":6}},{"id":254,"name":"Vi","stats":{"totalSessionsPlayed":1,"totalSessionsLost":0,"totalSessionsWon":1,"totalChampionKills":2,"totalDamageDealt":101554,"totalDamageTaken":18470,"mostChampionKillsPerSession":2,"totalMinionKills":122,"totalDoubleKills":0,"totalTripleKills":0,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":1,"totalGoldEarned":11358,"mostSpellsCast":0,"totalTurretsKilled":0,"totalPhysicalDamageDealt":81674,"totalMagicDamageDealt":18995,"totalFirstBlood":0,"totalAssists":15,"maxChampionsKilled":2,"maxNumDeaths":1}},{"id":103,"name":"Ahri","stats":{"totalSessionsPlayed":5,"totalSessionsLost":2,"totalSessionsWon":3,"totalChampionKills":33,"totalDamageDealt":583292,"totalDamageTaken":86957,"mostChampionKillsPerSession":10,"totalMinionKills":798,"totalDoubleKills":2,"totalTripleKills":0,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":22,"totalGoldEarned":55637,"mostSpellsCast":0,"totalTurretsKilled":0,"totalPhysicalDamageDealt":88954,"totalMagicDamageDealt":323163,"totalFirstBlood":0,"totalAssists":38,"maxChampionsKilled":10,"maxNumDeaths":7}},{"id":64,"name":"LeeSin","stats":{"totalSessionsPlayed":2,"totalSessionsLost":0,"totalSessionsWon":2,"totalChampionKills":15,"totalDamageDealt":283794,"totalDamageTaken":47208,"mostChampionKillsPerSession":8,"totalMinionKills":324,"totalDoubleKills":3,"totalTripleKills":1,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":7,"totalGoldEarned":26702,"mostSpellsCast":0,"totalTurretsKilled":2,"totalPhysicalDamageDealt":179409,"totalMagicDamageDealt":102365,"totalFirstBlood":0,"totalAssists":29,"maxChampionsKilled":8,"maxNumDeaths":4}},{"id":81,"name":"Ezreal","stats":{"totalSessionsPlayed":1,"totalSessionsLost":0,"totalSessionsWon":1,"totalChampionKills":10,"totalDamageDealt":168089,"totalDamageTaken":27079,"mostChampionKillsPerSession":10,"totalMinionKills":165,"totalDoubleKills":0,"totalTripleKills":0,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":8,"totalGoldEarned":15275,"mostSpellsCast":0,"totalTurretsKilled":3,"totalPhysicalDamageDealt":131121,"totalMagicDamageDealt":34756,"totalFirstBlood":0,"totalAssists":17,"maxChampionsKilled":10,"maxNumDeaths":8}},{"id":105,"name":"Fizz","stats":{"totalSessionsPlayed":1,"totalSessionsLost":1,"totalSessionsWon":0,"totalChampionKills":3,"totalDamageDealt":80294,"totalDamageTaken":21753,"mostChampionKillsPerSession":3,"totalMinionKills":114,"totalDoubleKills":0,"totalTripleKills":0,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":7,"totalGoldEarned":9389,"mostSpellsCast":0,"totalTurretsKilled":0,"totalPhysicalDamageDealt":19029,"totalMagicDamageDealt":60293,"totalFirstBlood":0,"totalAssists":12,"maxChampionsKilled":3,"maxNumDeaths":7}},{"id":2,"name":"Olaf","stats":{"totalSessionsPlayed":1,"totalSessionsLost":0,"totalSessionsWon":1,"totalChampionKills":5,"totalDamageDealt":183508,"totalDamageTaken":34625,"mostChampionKillsPerSession":5,"totalMinionKills":211,"totalDoubleKills":0,"totalTripleKills":0,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":7,"totalGoldEarned":14095,"mostSpellsCast":0,"totalTurretsKilled":3,"totalPhysicalDamageDealt":117878,"totalMagicDamageDealt":37453,"totalFirstBlood":0,"totalAssists":8,"maxChampionsKilled":5,"maxNumDeaths":7}},{"id":1,"name":"Annie","stats":{"totalSessionsPlayed":6,"totalSessionsLost":2,"totalSessionsWon":4,"totalChampionKills":49,"totalDamageDealt":905226,"totalDamageTaken":122780,"mostChampionKillsPerSession":12,"totalMinionKills":1045,"totalDoubleKills":5,"totalTripleKills":1,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":45,"totalGoldEarned":83361,"mostSpellsCast":0,"totalTurretsKilled":5,"totalPhysicalDamageDealt":84761,"totalMagicDamageDealt":813322,"totalFirstBlood":0,"totalAssists":53,"maxChampionsKilled":12,"maxNumDeaths":12}},{"id":0,"name":"Combined","stats":{"totalSessionsPlayed":18,"totalSessionsLost":6,"totalSessionsWon":12,"totalChampionKills":117,"killingSpree":60,"totalDamageDealt":2333304,"totalDamageTaken":380594,"mostChampionKillsPerSession":12,"totalMinionKills":2796,"totalDoubleKills":10,"totalTripleKills":2,"totalQuadraKills":0,"totalPentaKills":0,"totalUnrealKills":0,"totalDeathsPerSession":103,"totalGoldEarned":223105,"mostSpellsCast":0,"totalTurretsKilled":13,"totalPhysicalDamageDealt":709385,"totalMagicDamageDealt":1411334,"totalNeutralMinionsKilled":234,"totalFirstBlood":0,"totalAssists":184,"totalHeal":26727,"maxLargestKillingSpree":7,"maxLargestCriticalStrike":769,"maxChampionsKilled":12,"maxNumDeaths":12,"maxTimePlayed":3149,"maxTimeSpentLiving":1666,"normalGamesPlayed":0,"rankedSoloGamesPlayed":0,"rankedPremadeGamesPlayed":0,"botGamesPlayed":0}}]}{"summoners":[{"id":21554735,"name":"Amphios"}]}

我希望能够使用大量的这些数据,并将它们放在$ api_data ['username']和$ api_data ['level']等等,或者更好的方式......我仍然习惯于这样的大量数据。

2 个答案:

答案 0 :(得分:1)

使用json_decode函数将SJON字符串转换为PHP数组:

$api_data = json_decode($getsummoner);

答案 1 :(得分:1)

您可以像这样使用PHP json_decode函数;

$json = '{"id":21554735,"name":"Amphios","profileIconId":588,"summonerLevel":30,"revisionDate":1390725829000}';
$arr = json_decode($json, true);

echo "<xmp>".print_r($arr, true)."</xmp>";  // Display the contents of $arr.
echo "Name is {$arr['name']}";              // Display the contents of $arr['name'].

以上将输出以下内容;

Array
(
    [id] => 21554735
    [name] => Amphios
    [profileIconId] => 588
    [summonerLevel] => 30
    [revisionDate] => 1390725829000
)
Name is Amphios