php - Notice: Undefined offset & Trying to get property of non-object -
i trying pull data json_decode array, have done before time getting issues.
this json looks like.
{ "achievementpercentages": { "achievements": [ { "name": "tf_scout_long_distance_runner", "percent": 54.668815612792969 }, { "name": "tf_heavy_damage_taken", "percent": 47.104038238525391 }, { "name": "tf_get_consecutivekills_nodeaths", "percent": 44.668777465820312 }, { "name": "tf_pyro_camp_position", "percent": 36.480117797851563 }, { "name": "tf_kill_nemesis", "percent": 34.392494201660156 }, { "name": "tf_burn_playersinminimumtime", "percent": 33.580135345458984 }, { "name": "tf_pyro_burn_medicpair", "percent": 32.748367309570312 },
and code this
$achname = $decodedach->achievementpercentages->achievements[$achcounter]->name; $achpercent = $decodedach->achievementpercentages->achievements[$achcounter]->percent;
the $achcounter present in while statement, in effort achievements seperately. don't think part wrong, how trying access data in array. don't see wrong though.
any appreciated.
full while statement requested
while($achcounter < $responsecount) { $appid = $decodedsteam->response->games[$achcounter]->appid; $achievementurl = 'http://api.steampowered.com/isteamuserstats/getglobalachievementpercentagesforapp/v0002/?gameid='.$appid.'&format=json'; $jsonach = file_get_contents($achievementurl); $decodedach = json_decode($jsonach); //var_dump($decodedach); $achname = $decodedach->achievementpercentages->achievements[$achcounter]->name; $achpercent = $decodedach->achievementpercentages->achievements[$achcounter]->percent; //echo $achievementurl; $sql = "insert steamid".$steamid."(name, percent) values ('".$achname."','".$achpercent."')"; $achcounter++; }
you need iterate again after fetch data url. $achcounter usefull $decodedsteam
, not $decodedach
:
while($achcounter < $responsecount) { $appid = $decodedsteam->response->games[$achcounter]->appid; $achievementurl = 'http://api.steampowered.com/isteamuserstats/getglobalachievementpercentagesforapp/v0002/?gameid='.$appid.'&format=json'; $jsonach = file_get_contents($achievementurl); $decodedach = json_decode($jsonach); //var_dump($decodedach); foreach($decodedach->achievementpercentages->achievements $achievement) { //here $achname = $achievement->name; $achpercent = $achievement->percent; //echo $achievementurl; $sql = "insert steamid".$steamid."(name, percent) values ('".$achname."','".$achpercent."')"; } $achcounter++; }