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

LF-DevJourney
28.7k30 gold badges169 silver badges319 bronze badges
asked Jun 13, 2017 at 10:41
5
  • you have to use foreach to get that value out from that array Commented Jun 13, 2017 at 10:43
  • 1
    In its most basic form - $stats[0]->elo;. Commented Jun 13, 2017 at 10:44
  • 3
    Possible duplicate of Parsing JSON file with PHP Commented Jun 13, 2017 at 10:45
  • eval.in/816160 Commented Jun 13, 2017 at 10:46
  • Thanks been looking for ages could not find something similar Commented Jun 13, 2017 at 10:48

5 Answers 5

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;
}
answered Jun 13, 2017 at 10:46

Comments

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;

answered Jun 13, 2017 at 10:47

Comments

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);
answered Jun 13, 2017 at 10:45

Comments

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);
answered Jun 13, 2017 at 10:49

Comments

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']

answered Jun 13, 2017 at 10:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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