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条)

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色