0

ANSWER: PHP/JSON - stdClass Object

I tried to read some statistics from a json like this, but I make a mistake somewhere. I tried with foreach function, but i don't know where is the mistake:

foreach($myDecodedStringFromJSon->playerStatsSummaries as $stats){
 // Do some here
}

This is my JSON:

{
   "summonerId":39656713,
   "playerStatSummaries":[
      {
         "playerStatSummaryType":"CAP5x5",
         "wins":0,
         "modifyDate":1431942577000,
         "aggregatedStats":{
            "totalChampionKills":3,
            "totalMinionKills":79,
            "totalTurretsKilled":0,
            "totalNeutralMinionsKilled":0,
            "totalAssists":4
         }
      },
      {
         "playerStatSummaryType":"CoopVsAI",
         "wins":22,
         "modifyDate":1431942577000,
         "aggregatedStats":{
            "totalChampionKills":73,
            "totalMinionKills":2344,
            "totalTurretsKilled":24,
            "totalNeutralMinionsKilled":0,
            "totalAssists":179
         }
      },
      {
         "playerStatSummaryType":"OdinUnranked",
         "wins":0,
         "modifyDate":1431942577000,
         "aggregatedStats":{

         }
      },
      {
         "playerStatSummaryType":"Unranked",
         "wins":18,
         "modifyDate":1431942577000,
         "aggregatedStats":{
            "totalChampionKills":63,
            "totalMinionKills":1789,
            "totalTurretsKilled":14,
            "totalNeutralMinionsKilled":5,
            "totalAssists":181
         }
      },
      {
         "playerStatSummaryType":"Unranked3x3",
         "wins":0,
         "modifyDate":1431942577000,
         "aggregatedStats":{

         }
      },
      {
         "playerStatSummaryType":"AramUnranked5x5",
         "wins":38,
         "modifyDate":1445430660000,
         "aggregatedStats":{
            "totalChampionKills":414,
            "totalTurretsKilled":28,
            "totalAssists":1556
         }
      }
   ]
}

EDIT:

stdClass Object ( [summonerId] => 39656713 [playerStatSummaries] => Array ( [0] => stdClass Object ( [playerStatSummaryType] => CAP5x5 [wins] => 0 [modifyDate] => 1431942577000 [aggregatedStats] => stdClass Object ( [totalChampionKills] => 3 [totalMinionKills] => 79 [totalTurretsKilled] => 0 [totalNeutralMinionsKilled] => 0 [totalAssists] => 4 ) ) [1] => stdClass Object ( [playerStatSummaryType] => CoopVsAI [wins] => 22 [modifyDate] => 1431942577000 [aggregatedStats] => stdClass Object ( [totalChampionKills] => 73 [totalMinionKills] => 2344 [totalTurretsKilled] => 24 [totalNeutralMinionsKilled] => 0 [totalAssists] => 179 ) ) [2] => stdClass Object ( [playerStatSummaryType] => OdinUnranked [wins] => 0 [modifyDate] => 1431942577000 [aggregatedStats] => stdClass Object ( ) ) [3] => stdClass Object ( [playerStatSummaryType] => Unranked [wins] => 18 [modifyDate] => 1431942577000 [aggregatedStats] => stdClass Object ( [totalChampionKills] => 63 [totalMinionKills] => 1789 [totalTurretsKilled] => 14 [totalNeutralMinionsKilled] => 5 [totalAssists] => 181 ) ) [4] => stdClass Object ( [playerStatSummaryType] => Unranked3x3 [wins] => 0 [modifyDate] => 1431942577000 [aggregatedStats] => stdClass Object ( ) ) [5] => stdClass Object ( [playerStatSummaryType] => AramUnranked5x5 [wins] => 38 [modifyDate] => 1445430660000 [aggregatedStats] => stdClass Object ( [totalChampionKills] => 414 [totalTurretsKilled] => 28 [totalAssists] => 1556 ) ) ) ) 
3
  • could you provide print_r($myDecodedStringFromJSon) for us. But as far as i can guess you string was decoded as array $myDecodedStringFromJSon['playerStatsSummaries'] Commented Dec 17, 2015 at 14:11
  • I get the answer by myself: stackoverflow.com/questions/3754411/php-json-stdclass-object Commented Dec 17, 2015 at 14:16
  • @George-Cristian Yeah, All you need is the json_decode(string, true); As that turns it to an array. Commented Dec 17, 2015 at 14:36

1 Answer 1

0

Just decode the JSON and loop using the foreach. The below code will help you.

$result = json_decode($json);
  foreach ($result->playerStatSummaries as $stats) {
  echo 'Wins: ' . $stats->wins;
  echo '<br>';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.