Action at each PHP loop

0

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

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);
    }
}
    
asked by anonymous 08.09.2018 / 21:58

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);
    }
}

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

Note: I put <= in the comparison, otherwise, I would not get the last 2399 .

    
08.09.2018 / 22:07