PHP code example of xuqinqin / php-chunker
1. Go to this page and download the library: Download xuqinqin/php-chunker library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xuqinqin / php-chunker example snippets
use Exp\Chunker\ArrayChunker;
$arr = [1, 2, 3, 4, 5, 6, 7];
$chunker = new ArrayChunker($arr, count($arr));
$chunker->batch(function($batch) {
// [1, 2], [3, 4], [5, 6], [7]
})->each(function($item) {
// 1, 2, 3, 4, 5, 6, 7
})->chunk(2);
use Exp\Chunker\CollectionChunker;
$collection = collect([1, 2, 3, 4, 5, 6, 7]);
$chunker = new CollectionChunker($collection, $collection->count());
$chunker->batch(function ($batch) {
// collect([1, 2, 3]), collect([4, 5, 6]), collect([7])
})->each(function ($item) {
// 1, 2, 3, 4, 5, 6, 7
})->chunk(3);
use Exp\Chunker\QueryChunker;
$query = User::query(); // or DB::table('user');
$chunker = new QueryChunker($query, $query->count());
$chunker->batch(function ($batch) {
// $batch is Collection|User[]
})->each(function (User $user) {
// 1, 2, 3, 4, 5, 6, 7
})->chunk(3);
use Exp\Chunker\FileChunker;
$file = 'xxx.txt';
$chunker = new FileChunker($file);
$chunker->batch(function ($lines) {
// handle lines
})->each(function ($line) {
// handle line
})->chunk(3);
$chunker->showProgress()->chunk(3);
$arr = [1, 2, 3, 4, 5, 6, 7];
$chunker = new ArrayChunker($arr, count($arr));
$chunker->fixed()->batch(function ($batch) {
// handle batch
})->each(function (ItemValue $item) {
echo $item->getValue()."\n";
$item->setValue($item->getValue() + 1);
})->where(function (ItemValue $item) {
return $item->getValue() < 5;
})->chunk(3); // $chunker->source = 5, 5, 5, 5, 5, 6, 7
$collection = collect([1, 2, 3, 4, 5, 6, 7]);
$chunker = new CollectionChunker($collection, $collection->count());
$chunker->fixed()->where(function (ItemValue $itemValue) {
return $itemValue->getValue() > 3;
})->each(function (ItemValue $itemValue) {
echo $itemValue->getValue()."\n";
$itemValue->setValue($itemValue->getValue() - 1);
})->chunk(2); // $chunker->source = 1, 2, 3, 3, 3, 3, 3
$query = User::query()->where('is_login', 1);
$chunker = new QueryChunker($query, $query->count());
$chunker->fixed()->each(function (User $user) {
$user->is_login = 0;
$user->save();
})->chunk(3);