0

I have this code using an api , the first iteration is showing everything right , but at the second iteration is showing the items from the first iteration + the items from the second iteration , and the same thing at the third iteration. I dont want that , how can i fix that problem ?

$g=0;

foreach ($matches as $match) {

$inside = $api->getMatch('123');

$pp = $api->getMatchTimeline('123');


foreach ($inside->participants as $partId) {

if ($partId->championId == $match->champion) {

 $participant_id[] = $partId->participantId;
 $participant_idS = $partId->stats->participantId;
 foreach ($pp->frames as $p) {
 foreach ($p->events as $t) {
 if ($t->type == "ITEM_PURCHASED" and $t->participantId == $participant_idS) {
 $item_id = $t->itemId;

 $d = $api->getStaticItem($item_id);

 if($d->depth == 2 or $d->depth == 3){

 $itemsMade[] = $d->id;

         }
        }
       }
      }               
     }
    }

 $dt = [['match_ids' => $part, "champion" => $soloq->champion, "timestamp" => $match->timestamp, "participantId" => $participant_id[$g++], "itens" => json_encode($itemsMade)]
                    ];

                    echo ' <pre>';
                    var_dump($dt);
                    echo  '</pre>';

 }

enter image description here

0

1 Answer 1

1

You need to set that array to be empty at the start of your loop. Otherwise you just keep adding to it as you see in your output.

foreach ($inside->participants as $partId) {
    $itemsMade = [];
1
  • thanks ! it worked , but i set the array at the first foreach ($matches as $match) {
    – Lucas Ln
    Jan 31, 2021 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.