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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?