I've got an API I'm working with that accepts a call every 5 seconds, any more than that it wont respond. In each call it will accept 5 records in a batch. I've got a list of 1000s of records that I need to check using the api, so what I'm trying to do is send it my list of records in broken up into batches of 5 every 5 seconds.
I can get most of it to work, but the bit I can't figure is breaking down the list of records which is an array in batches, any idea how to do this?
This is the code I was using below, but it's outputting each individual part of the array every 5 seconds, rather than in batches of 5.
$my_array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
foreach ($my_array as $key => $value) {
sleep (5);
echo $value;
}
PHP array_chunk() Function The array_chunk() function takes an array as input and split that array into smaller chunks of the given size. The last chunk may contain less number of elements than passed size based on the multiplicity factor of the total numbers available in the array.
The foreach() method is used to loop through the elements in an indexed or associative array. It can also be used to iterate over objects. This allows you to run blocks of code for each element.
you could have a second loop of
$batch_of = 5;
$batch = array_chunk($my_array, $batch_of);
foreach($batch as $b) {
foreach ($b as $key => $value) {
echo $value;
}
sleep (5);
}
Which would work as intended
In case it helps anyone else, I wrote a function that will allow you to process an array in chunks. The description and details are here:
https://totaldev.com/php-process-arrays-batches/
The main difference between mine and array_chunk
is that mine doesn't return an array of smaller arrays. It takes a user-defined function as a closure that will handle the small batches. Here is the function:
// Iterate through an array and pass batches to a Closure
function arrayBatch($arr, $batchSize, $closure) {
$batch = [];
foreach($arr as $i) {
$batch[] = $i;
// See if we have the right amount in the batch
if(count($batch) === $batchSize) {
// Pass the batch into the Closure
$closure($batch);
// Reset the batch
$batch = [];
}
}
// See if we have any leftover ids to process
if(count($batch)) $closure($batch);
}
You can use it like this:
// Use array in batches
arrayBatch($my_array, 5, function($batch) {
// Do whataver you need to with the $batch of 5 items here...
sleep (5);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With