Closed Lucasnl closed 3 years ago
Well what is happening here is that the $onSuccess
function gets called for each fetched match entry. So if you print the result in that function you will always be printing values for the single match only, you could do this to fix that:
$kills = 0;
$onSuccess = function (Objects\MatchDto $match) use ($kills) {
foreach ($match->participants as $p)
if ($p->participantId==1 && $p->teamId==100)
$kills += $p->stats->kills;
};
// $onFailure = function ...
// foreach ($custom as $games) ...
// $api->commitAsync();
var_dump($kills);
echo "<br>";
This will aggregate the kills in the variable and after the commit is finished and all matches processed you print out the result.
Thank you very much for the help ! i tried this and is returning 0;
Hey @Lucasnl, the last missing piece of the puzzle is that function (…) use ($var)
does pass-by-value for $var
, the only thing you need to do now is to add magical &
which makes PHP to pass the $var
by reference to the function which results in variable's value being correctly updated.
Fully working snippet:
$kills = 0;
$onSuccess = function (RiotAPI\LeagueAPI\Objects\MatchDto $match) use (&$kills) {
foreach ($match->participants as $p)
if ($p->participantId == 1 && $p->teamId == 100)
$kills += $p->stats->kills;
};
$onFailure = function ($ex) {
echo "Error occured: {$ex->getMessage()}\n";
};
foreach ($custom as $games) {
$api->nextAsync($onSuccess, $onFailure);
$match = $api->getMatch($games->match_id);
}
$api->commitAsync();
var_dump($kills);
Thanks again for your time and help ! it worked very well
im trying to get the sum of all kills from the participant id =1 , but it seems that array_sum is not working properly inside the $onsucess function , im getting only the kills for each match and not the sum of them all.
do you have any tips for me ?
im getting the kills from each game but the expected result is 122 , the sum of all those values