End every PHP loop

Asked

Viewed 29 times

0

I have a query in sql returning 2399 records, I divide and run it by parts, I need to find a way to divide the loop equally, because if I divide by 100 example, it still remains a 99 loop.

for ($i = 1; $i < 2399; $i++) {

    $promises[] = $client->postAsync("/localize/1002/consultar", ['form_params' => ['cpf' => $fetch[$i]->cpf]]);

    if ($i % 100 == 0) {
        //$pr($promises);
        //unset($promises);
    }
}
  • Explain better what you want, because it seems that the problem is different and should not even be doing this.

1 answer

1


You can add an auxiliary variable:

$rows = 2399;

for ($i = 1; $i <= $rows; $i++) {

    $promises[] = $client->postAsync("/localize/1002/consultar", ['form_params' => ['cpf' => $fetch[$i]->cpf]]);

    if ($i % 100 == 0 || $i == $rows) {
        //$pr($promises);
        //unset($promises);
    }
}

Thus, putting a OR ( || ), always on the last record, will enter the condition ( if ).

Note: I put <= in comparison, if not, would not catch the last 2399.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.