doudou1309 2016-04-02 15:53
浏览 10

获取有时存在的变量,而不是在其他时间存在

My problem is in my code I'm having problems with it calculating something with a variable that does not exists some times and some times does.

I want it to give me back number of deaths if empty '0' if not prints me what is in the variable, but for some reason i get this:

E_NOTICE : type 8 -- Undefined property: stdClass::$numDeaths -- at line 66 E_WARNING : type 2 -- Division by zero -- at line 71

here is my code:

<?php
$apiKey = 'e9044828-20e3-46cc-9eb5-545949299803';
$summonerName = 'tamini';
$new = rawurlencode($summonerName);

$news = str_replace(' ', '', $summonerName);
$str = strtolower($news);



// get the basic summoner info
$result = file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/' . $new . '?api_key=' . $apiKey);
$summoner = json_decode($result)->$str;
$id = $summoner->id;
// var_dump($summoner);
?>    


<?php   
$clawzz = file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.3/game/by-summoner/' . $id . '/recent?api_key=' . $apiKey);
$gazazz = json_decode($clawzz);
?>


<?php 




$entrieszz = $gazazz->games;
usort($entrieszz, function($ac,$bc){
return $bc->createDate-$ac->createDate;
});


foreach($entrieszz as $statSummaryzz){

$gamemodekillz = $statSummaryzz->stats->championsKilled;

$gamemodedeathz = $statSummaryzz->stats->numDeaths;

$gamemodeassistz = $statSummaryzz->stats->assists;


$kdamatchhistoryeach = ($gamemodekillz + $gamemodeassistz) /    $gamemodedeathz;


   echo '  <br>';

        echo $gamemodekillz;

echo '  <br>';


    if ($gamemodedeathz == 0){

     echo '0';

    }
    else {

    echo $gamemodedeathz ;
    }



echo '  <br>';

        echo $gamemodeassistz;

echo '  <br>';



    if ($gamemodedeathz == 0){

     echo 'Perfect Score';

    }
    else {

    echo $kdamatchhistoryeach ;
    }
?>
  • 写回答

1条回答 默认 最新

  • dongpu9481 2016-04-02 15:57
    关注

    You need to check if the value exist:

    if( isset($statSummaryzz->stats) && isset($statSummaryzz->stats->numDeaths) ) {
        $gamemodedeathz = $statSummaryzz->stats->numDeaths;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集