PHP code example of abordji / php-generator-chunk

1. Go to this page and download the library: Download abordji/php-generator-chunk 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/ */

    

abordji / php-generator-chunk example snippets




// Example usage:
$sourceGenerator = (function () {
    yield 'a' => 1;
    yield 'b' => 2;
    yield 'c' => 3;
    yield 'd' => 4;
    yield 'e' => 5;
})();

$chunkedGenerator = generator_chunk($sourceGenerator, 2, true); // Chunk size 2, preserve keys

echo "Processing chunked generator:\n";
foreach ($chunkedGenerator as $index => $chunk) {
    echo "Chunk " . ($index + 1) . ": [";
    $items = [];
    // $chunk is also a Generator
    foreach ($chunk as $key => $value) {
        $items[] = $key . ':' . $value;
    }
    echo implode(', ', $items) . "]\n";
}
// Output:
// Processing chunked generator:
// Chunk 1: [a:1, b:2]
// Chunk 2: [c:3, d:4]
// Chunk 3: [e:5]
bash
composer