phpriot-games-api

How to echo PHP HashMaps


I'm new in PHP developing and need your help. I write a web page summoner-info.com with the RIOT API. but I'm too bad to understand the documentation. I want to output via echo my states. In the API docs stands:

Return Value: Map[string, List[LeagueDto]]

But I don't understand how to use this.

Doc link: link

I wrote this:

$url = "https://{$region}.api.pvp.net/api/lol/{$region}/v2.5/league/by-summoner/{$summoner_ID}?api_key={$api}";
$data = file_get_contents($url);
$data = json_decode($data, true);
print_r($data);

So how can I write something like this

echo $data["tier"["LeagueDto "]]

Solution

  • Assuming this is the kind of response you're expecting (2 summoner ids):

    https://github.com/josephyi/taric/blob/master/spec/fixtures/leagues_by_summoner_ids.json

    There's no LeagueDto entry in the JSON response. When Riot refers to 'LeagueDto' that's the class that represents the data of the object, but is not meant to be accessed from the response. If you look at the response, you'll have to navigate the JSON. I don't know PHP, but assuming you want summoner id 21066:

    $data["21066"] // array of leagues the summoner is in
    $data["21066"][0] // first league the summoner is in
    $data["21066"][0]["entries"] // array of league entries for the first league
    $data["21066"][0]["tier"] // tier of first league
    

    Hope that helps!