im stuck in getting json response to array variables in my code so
1. The thing that i make work is pure JSON from link:
{"39766855": "id":39766855,"name":"MCNoAd","profileIconId":779,"summonerLevel":30,"revisionDate":1432371417000}}
My code:
$KEY = "<my key>"; //hidden for security
$url='https://eune.api.pvp.net/api/lol/eune/v1.4/summoner/by-name/'.$summoner.'?api_key='.$KEY;
$data=file_get_contents($url);
$json=json_decode($data);
$lol_summoner_array = (array) $json; // dont know if it is a best method but it do the work
$id = $lol_summoner_array[$summoner]->id; //set for future use
And now i can call the response values by:
$lol_summoner_array[$summoner]->summonerLevel // output: 30
$lol_summoner_array[$summoner]->name // output MCNoAd
it works good but i can't handle with my following code : 1st. my json response is like:
{"summonerId":39766855,"playerStatSummaries":[{"playerStatSummaryType":"CoopVsAI","wins":56,"modifyDate":1423415169000,"aggregatedStats":{"totalChampionKills":304,"totalMinionKills":4541,"totalTurretsKilled":80,"totalNeutralMinionsKilled":158,"totalAssists":506}},{"playerStatSummaryType":"Unranked3x3","wins":4,"modifyDate":1423415169000,"aggregatedStats":{"totalChampionKills":26,"totalMinionKills":474,"totalTurretsKilled":3,"totalNeutralMinionsKilled":11,"totalAssists":43}},{"playerStatSummaryType":"RankedSolo5x5","wins":15,"losses":29,"modifyDate":1430810672000,"aggregatedStats":{"totalChampionKills":100,"totalMinionKills":2239,"totalTurretsKilled":20,"totalNeutralMinionsKilled":147,"totalAssists":501}},{"playerStatSummaryType":"OdinUnranked","wins":3,"modifyDate":1431076606000,"aggregatedStats":{"totalChampionKills":32,"totalAssists":37,"maxChampionsKilled":7,"averageNodeCapture":7,"averageNodeNeutralize":5,"averageTeamObjective":1,"averageTotalPlayerScore":854,"averageCombatPlayerScore":207,"averageObjectivePlayerScore":648,"averageNodeCaptureAssist":0,"averageNodeNeutralizeAssist":2,"maxNodeCapture":10,"maxNodeNeutralize":8,"maxTeamObjective":1,"maxTotalPlayerScore":1147,"maxCombatPlayerScore":298,"maxObjectivePlayerScore":944,"maxNodeCaptureAssist":1,"maxNodeNeutralizeAssist":2,"totalNodeNeutralize":31,"totalNodeCapture":44,"averageChampionsKilled":4,"averageNumDeaths":7,"averageAssists":6,"maxAssists":10}},{"playerStatSummaryType":"CAP5x5","wins":68,"modifyDate":1431585301000,"aggregatedStats":{"totalChampionKills":541,"totalMinionKills":9928,"totalTurretsKilled":100,"totalNeutralMinionsKilled":828,"totalAssists":1991}},{"playerStatSummaryType":"CounterPick","wins":0,"modifyDate":1431689669000,"aggregatedStats":{"totalChampionKills":1,"totalMinionKills":31,"totalTurretsKilled":0,"totalNeutralMinionsKilled":0,"totalAssists":12}},{"playerStatSummaryType":"AramUnranked5x5","wins":40,"modifyDate":1432364514000,"aggregatedStats":{"totalChampionKills":543,"totalTurretsKilled":29,"totalAssists":1797}},{"playerStatSummaryType":"URF","wins":14,"modifyDate":1432364514000,"aggregatedStats":{"totalChampionKills":120,"totalMinionKills":1598,"totalTurretsKilled":32,"totalNeutralMinionsKilled":55,"totalAssists":224}},{"playerStatSummaryType":"Unranked","wins":146,"modifyDate":1432371416000,"aggregatedStats":{"totalChampionKills":1327,"totalMinionKills":26048,"totalTurretsKilled":252,"totalNeutralMinionsKilled":365,"totalAssists":2976}}]}
And my php is:
$url2='https://eune.api.pvp.net/api/lol/eune/v1.3/stats/by-summoner/'.$id.'/ranked?api_key='.$KEY;
$data2=file_get_contents($url2);
$json2=json_decode($data2);
$lol_stats_array = (array) $json2;
echo '<br> test:'.$lol_stats_array[$id][0]->totalSessionsPlayed .'<br>'; // outputs nothing
print_r($lol_stats_array); // output below
my array:
Array ( [summonerId] => 39766855 [modifyDate] => 1430810672000 [champions] => Array ( [0] => stdClass Object ( [id] => 76 [stats] => stdClass Object ( [totalSessionsPlayed] => 1 [totalSessionsLost] => 1 [totalSessionsWon] => 0 [totalChampionKills] => 1 [totalDamageDealt] => 94996 [totalDamageTaken] => 28774 [mostChampionKillsPerSession] => 1 [totalMinionKills] => 57 [totalDoubleKills] => 0 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 13 [totalGoldEarned] => 8518 [mostSpellsCast] => 0 [totalTurretsKilled] => 0 [totalPhysicalDamageDealt] => 20123 [totalMagicDamageDealt] => 63373 [totalFirstBlood] => 0 [totalAssists] => 4 [maxChampionsKilled] => 1 [maxNumDeaths] => 13 ) ) [1] => stdClass Object ( [id] => 89 [stats] => stdClass Object ( [totalSessionsPlayed] => 8 [totalSessionsLost] => 4 [totalSessionsWon] => 4 [totalChampionKills] => 10 [totalDamageDealt] => 224816 [totalDamageTaken] => 152287 [mostChampionKillsPerSession] => 3 [totalMinionKills] => 223 [totalDoubleKills] => 0 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 54 [totalGoldEarned] => 53322 [mostSpellsCast] => 0 [totalTurretsKilled] => 3 [totalPhysicalDamageDealt] => 68075 [totalMagicDamageDealt] => 138066 [totalFirstBlood] => 0 [totalAssists] => 70 [maxChampionsKilled] => 3 [maxNumDeaths] => 10 ) ) [2] => stdClass Object ( [id] => 412 [stats] => stdClass Object ( [totalSessionsPlayed] => 1 [totalSessionsLost] => 1 [totalSessionsWon] => 0 [totalChampionKills] => 4 [totalDamageDealt] => 175991 [totalDamageTaken] => 40536 [mostChampionKillsPerSession] => 4 [totalMinionKills] => 164 [totalDoubleKills] => 0 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 9 [totalGoldEarned] => 16185 [mostSpellsCast] => 0 [totalTurretsKilled] => 0 [totalPhysicalDamageDealt] => 31031 [totalMagicDamageDealt] => 144959 [totalFirstBlood] => 0 [totalAssists] => 17 [maxChampionsKilled] => 4 [maxNumDeaths] => 9 ) ) [3] => stdClass Object ( [id] => 53 [stats] => stdClass Object ( [totalSessionsPlayed] => 7 [totalSessionsLost] => 4 [totalSessionsWon] => 3 [totalChampionKills] => 13 [totalDamageDealt] => 259541 [totalDamageTaken] => 138600 [mostChampionKillsPerSession] => 6 [totalMinionKills] => 178 [totalDoubleKills] => 0 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 42 [totalGoldEarned] => 56782 [mostSpellsCast] => 0 [totalTurretsKilled] => 4 [totalPhysicalDamageDealt] => 84190 [totalMagicDamageDealt] => 175009 [totalFirstBlood] => 0 [totalAssists] => 72 [maxChampionsKilled] => 6 [maxNumDeaths] => 10 ) ) [4] => stdClass Object ( [id] => 25 [stats] => stdClass Object ( [totalSessionsPlayed] => 7 [totalSessionsLost] => 6 [totalSessionsWon] => 1 [totalChampionKills] => 20 [totalDamageDealt] => 414609 [totalDamageTaken] => 176570 [mostChampionKillsPerSession] => 6 [totalMinionKills] => 452 [totalDoubleKills] => 0 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 68 [totalGoldEarned] => 65223 [mostSpellsCast] => 0 [totalTurretsKilled] => 2 [totalPhysicalDamageDealt] => 86037 [totalMagicDamageDealt] => 328319 [totalFirstBlood] => 0 [totalAssists] => 65 [maxChampionsKilled] => 6 [maxNumDeaths] => 12 ) ) [5] => stdClass Object ( [id] => 37 [stats] => stdClass Object ( [totalSessionsPlayed] => 10 [totalSessionsLost] => 4 [totalSessionsWon] => 6 [totalChampionKills] => 25 [totalDamageDealt] => 469526 [totalDamageTaken] => 231924 [mostChampionKillsPerSession] => 5 [totalMinionKills] => 307 [totalDoubleKills] => 2 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 90 [totalGoldEarned] => 111032 [mostSpellsCast] => 0 [totalTurretsKilled] => 4 [totalPhysicalDamageDealt] => 133894 [totalMagicDamageDealt] => 333833 [totalFirstBlood] => 0 [totalAssists] => 205 [maxChampionsKilled] => 5 [maxNumDeaths] => 15 ) ) [6] => stdClass Object ( [id] => 54 [stats] => stdClass Object ( [totalSessionsPlayed] => 3 [totalSessionsLost] => 3 [totalSessionsWon] => 0 [totalChampionKills] => 2 [totalDamageDealt] => 252006 [totalDamageTaken] => 75940 [mostChampionKillsPerSession] => 1 [totalMinionKills] => 291 [totalDoubleKills] => 0 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 24 [totalGoldEarned] => 23376 [mostSpellsCast] => 0 [totalTurretsKilled] => 4 [totalPhysicalDamageDealt] => 89225 [totalMagicDamageDealt] => 162697 [totalFirstBlood] => 0 [totalAssists] => 21 [maxChampionsKilled] => 1 [maxNumDeaths] => 9 ) ) [7] => stdClass Object ( [id] => 17 [stats] => stdClass Object ( [totalSessionsPlayed] => 1 [totalSessionsLost] => 0 [totalSessionsWon] => 1 [totalChampionKills] => 8 [totalDamageDealt] => 66593 [totalDamageTaken] => 20088 [mostChampionKillsPerSession] => 8 [totalMinionKills] => 87 [totalDoubleKills] => 0 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 7 [totalGoldEarned] => 11336 [mostSpellsCast] => 0 [totalTurretsKilled] => 1 [totalPhysicalDamageDealt] => 31710 [totalMagicDamageDealt] => 34707 [totalFirstBlood] => 0 [totalAssists] => 10 [maxChampionsKilled] => 8 [maxNumDeaths] => 7 ) ) [8] => stdClass Object ( [id] => 33 [stats] => stdClass Object ( [totalSessionsPlayed] => 1 [totalSessionsLost] => 1 [totalSessionsWon] => 0 [totalChampionKills] => 1 [totalDamageDealt] => 132918 [totalDamageTaken] => 38452 [mostChampionKillsPerSession] => 1 [totalMinionKills] => 165 [totalDoubleKills] => 0 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 9 [totalGoldEarned] => 11435 [mostSpellsCast] => 0 [totalTurretsKilled] => 1 [totalPhysicalDamageDealt] => 52178 [totalMagicDamageDealt] => 80739 [totalFirstBlood] => 0 [totalAssists] => 10 [maxChampionsKilled] => 1 [maxNumDeaths] => 9 ) ) [9] => stdClass Object ( [id] => 18 [stats] => stdClass Object ( [totalSessionsPlayed] => 2 [totalSessionsLost] => 2 [totalSessionsWon] => 0 [totalChampionKills] => 13 [totalDamageDealt] => 192425 [totalDamageTaken] => 52648 [mostChampionKillsPerSession] => 9 [totalMinionKills] => 210 [totalDoubleKills] => 2 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 22 [totalGoldEarned] => 21002 [mostSpellsCast] => 0 [totalTurretsKilled] => 1 [totalPhysicalDamageDealt] => 157131 [totalMagicDamageDealt] => 35243 [totalFirstBlood] => 0 [totalAssists] => 15 [maxChampionsKilled] => 9 [maxNumDeaths] => 13 ) ) [10] => stdClass Object ( [id] => 32 [stats] => stdClass Object ( [totalSessionsPlayed] => 2 [totalSessionsLost] => 2 [totalSessionsWon] => 0 [totalChampionKills] => 3 [totalDamageDealt] => 153354 [totalDamageTaken] => 35500 [mostChampionKillsPerSession] => 3 [totalMinionKills] => 53 [totalDoubleKills] => 1 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 11 [totalGoldEarned] => 13189 [mostSpellsCast] => 0 [totalTurretsKilled] => 0 [totalPhysicalDamageDealt] => 23862 [totalMagicDamageDealt] => 111828 [totalFirstBlood] => 0 [totalAssists] => 5 [maxChampionsKilled] => 3 [maxNumDeaths] => 6 ) ) [11] => stdClass Object ( [id] => 201 [stats] => stdClass Object ( [totalSessionsPlayed] => 1 [totalSessionsLost] => 1 [totalSessionsWon] => 0 [totalChampionKills] => 0 [totalDamageDealt] => 39841 [totalDamageTaken] => 34488 [mostChampionKillsPerSession] => 0 [totalMinionKills] => 52 [totalDoubleKills] => 0 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 11 [totalGoldEarned] => 7745 [mostSpellsCast] => 0 [totalTurretsKilled] => 0 [totalPhysicalDamageDealt] => 10585 [totalMagicDamageDealt] => 23995 [totalFirstBlood] => 0 [totalAssists] => 7 [maxChampionsKilled] => 0 [maxNumDeaths] => 11 ) ) [12] => stdClass Object ( [id] => 0 [stats] => stdClass Object ( [totalSessionsPlayed] => 44 [totalSessionsLost] => 29 [totalSessionsWon] => 15 [totalChampionKills] => 100 [killingSpree] => 35 [totalDamageDealt] => 2476616 [totalDamageTaken] => 1025807 [mostChampionKillsPerSession] => 9 [totalMinionKills] => 2239 [totalDoubleKills] => 5 [totalTripleKills] => 0 [totalQuadraKills] => 0 [totalPentaKills] => 0 [totalUnrealKills] => 0 [totalDeathsPerSession] => 360 [totalGoldEarned] => 399145 [mostSpellsCast] => 0 [totalTurretsKilled] => 20 [totalPhysicalDamageDealt] => 788041 [totalMagicDamageDealt] => 1632768 [totalNeutralMinionsKilled] => 147 [totalFirstBlood] => 0 [totalAssists] => 501 [totalHeal] => 177084 [maxLargestKillingSpree] => 4 [maxLargestCriticalStrike] => 911 [maxChampionsKilled] => 9 [maxNumDeaths] => 15 [maxTimePlayed] => 3144 [maxTimeSpentLiving] => 1140 [normalGamesPlayed] => 0 [rankedSoloGamesPlayed] => 0 [rankedPremadeGamesPlayed] => 0 [botGamesPlayed] => 0 ) ) ) )
i'm trying to display specified data for example "totalSessionsPlayed" of champion id 76 what is correct ? i'v tried $lol_stats_array[$id][76]->totalSessionsPlayed but nothing work.
答案 0 :(得分:1)
You will have to find the ID 76
from the champions list first, then you can get specific data from the filtered object. I have shown how to get totalSessionsPlayed
here. You can find other field similar way.
foreach($lol_stats_array['champions'] as $stat_object) {
if($stat_object->id == 76) {
$totalSessionsPlayed = $stat_object->stats->totalSessionsPlayed;
}
}
答案 1 :(得分:0)
$json = json_decode($data, true);
Pass in the second boolean parameter true
to get an array back. Then the lines where you attempt to cast $json into an array are unnecessary.
In your second example you could then use plain array notation instead of mixing array and object property notation. So this:
$lol_stats_array[$id][76]->totalSessionsPlayed
becomes this:
$lol_stats_array[$id][76]['totalSessionsPlayed']