0

I am trying to get the data out of this JSON format to be able to show on screen problem is I am not sure how I would be able to do this as I have tried multiple ways such as :

$stats = json_decode($result);
// var_dump($stats);
echo $stats->elo;

And nothing is working I am unsure how to get the data due to the [], as I have never worked with this before. As shown below is a small piece of the data I need to be able to get into.

[{"_id":{"championId":51,"role":"DUO_CARRY"},"elo":"BRONZE","patch":"7.11","championId":51,"positions":{"deaths":3,"winRates":6,"minionsKilled":2,"previousOverallPerformanceScore":6}}]

Thanks in advance

LF00
  • 27,015
  • 29
  • 156
  • 295

5 Answers5

1

There is a simple rule of thumb when processing JSON. First just decode it and print it using a print_r() so you can see its structure easily

$s = '[{"_id":{"championId":51,"role":"DUO_CARRY"},"elo":"BRONZE","patch":"7.11","championId":51,"positions":{"deaths":3,"winRates":6,"minionsKilled":2,"previousOverallPerformanceScore":6}}]';

$stats = json_decode($s);

print_r($stats);

Which will show you in this case

Array
(
    [0] => stdClass Object
        (
            [_id] => stdClass Object
                (
                    [championId] => 51
                    [role] => DUO_CARRY
                )
            [elo] => BRONZE
            [patch] => 7.11
            [championId] => 51
            [positions] => stdClass Object
                (
                    [deaths] => 3
                    [winRates] => 6
                    [minionsKilled] => 2
                    [previousOverallPerformanceScore] => 6
                )
        )
)

So now you know there is an array containing in this case only one object

So to show elo you could do a simple

echo $stats[0]->elo;    // BRONZE

But as it is an array of object it may be better to assume in some cases there will be more than one stat so you could process it in a foreach loop like this

foreach ($stats as $stat) {
    echo $stat->elo;
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
1

If you get more than one array data in that json use foreach as follow

foreach ($stats as $row) {
    echo $row->elo;
}

if you want to get only first record then use $stats[0]->elo;

0

Don't konw if i understood you correctly (you want to get data in array ? ) , but try

$stats = json_decode($result,true);
var_dump($stats);
Michał G
  • 2,234
  • 19
  • 27
0

Your output of json_decode is an array of objects. So you have to use index to access the array element first, then access the property of object with $array[0]->elo Live demo.

$string = '[{"_id":{"championId":51,"role":"DUO_CARRY"},"elo":"BRONZE","patch":"7.11","championId":51,"positions":{"deaths":3,"winRates":6,"minionsKilled":2,"previousOverallPerformanceScore":6}}]';
print_r(json_decode($string)[0]->elo);
LF00
  • 27,015
  • 29
  • 156
  • 295
0

If you want to get associative array you should use $stats = json_decode($result, true); var_dump($stats); And you'll get

array (size=1)
  0 => 
    array (size=5)
      '_id' => 
        array (size=2)
          'championId' => int 51
          'role' => string 'DUO_CARRY' (length=9)
      'elo' => string 'BRONZE' (length=6)
      'patch' => string '7.11' (length=4)
      'championId' => int 51
      'positions' => 
        array (size=4)
          'deaths' => int 3
          'winRates' => int 6
          'minionsKilled' => int 2
          'previousOverallPerformanceScore' => int 6

For getting elements get the first key of array by $stats[0] or with loop. Ex. $stats[0]['elo']

Hayk Manasyan
  • 508
  • 2
  • 20