nibb11
nibb11

Reputation: 25

Split lists in batches

This may sound simple but sometimes its the simple things that are quite harder to achieve. At least I can't find a proper way to do this clean with PHP and I searched different functions.

I have a lists of items I read from a file. For example:

foreach(file('items.txt') as $line) {
// Do something here with $line;
}

Assuming this text list (which is read line by line) has a specific number of items, 50, or 10,000 or 9,444, how can I split them into smaller batches?

For example split them into batches of 100 each. I basically want to read items.txt and write the items into smaller files, for example items1.txt, items2.txt, items3.txt, and so on. Each containing the same number of items, except the last file that contain less as we don't know the total numbers of items to split.

Upvotes: 0

Views: 590

Answers (2)

Morten Hekkvang
Morten Hekkvang

Reputation: 356

I would suggest you have a counter variable which counts how many lines you’ve added. Whenever it reaches your threshold you should do something with that data and afterwards clean the array and reset the counter to zero.

This also depends on what you’re trying to achieve. If your’re getting an out of memory error half way through, due to something like sending an email every iteration. You might solve it by adding a delay (sleep) after every action.

Hope this helps you.

Upvotes: 0

Erik Kalkoken
Erik Kalkoken

Reputation: 32777

You can use array_chunk() to automatically split your array of lines into sub-arrays of a specific size.

Examples

$chunks = array_chunk(file('items.txt'), 100);
foreach ($chunks as $chunk) {
   foreach($chunk as $line) {
   // Do something here with $line;
   }
}

Upvotes: 3

Related Questions