douxian9060 2015-04-02 09:29
浏览 28
已采纳

PHP | 访问对象中的对象

Hi I am very new to using API's with PHP. This is my first attempt. So far I have got the data being returned in a JSON format. I have the json_decoded() it and the var_dump() is returning what I can only work out as an object within an object. My PHP inexperience might be showing here.

Below is the code I am using which is successfully retrieving data.

    $url = 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/elderofaegis?api_key=RemovedForSO';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_URL, $url);
    $output = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($output);
    var_dump($result);

Below is what the var_dump() is returning

object(stdClass)[1]
  public 'elderofaegis' => 
     object(stdClass)[2]
      public 'id' => int 38186794
      public 'name' => string 'ElderOfAegis' (length=12)
      public 'profileIconId' => int 785
      public 'summonerLevel' => int 30
      public 'revisionDate' => float 1427933098000

Does anyone know how I could access the object by its name. By this i mean the sections contained within the quotes for example if i want to echo the name i want something along the lines of (Pseudo Below).

echo $result->object['name'];

The above example is Pseudo Because I can't figure it out. For anyone wondering this is the League Of Legends API

Thanks.

  • 写回答

2条回答 默认 最新

  • duanaoyuan7202 2015-04-02 09:32
    关注

    Try

    $result->elderofaegis->name;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?