1. Go to this page and download the library: Download gielfeldt/iterators 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/ */
gielfeldt / iterators example snippets
use Gielfeldt\Iterators\CachingIterator;
$input = new \ArrayIterator(range(1, 4));
$input = new \NoRewindIterator($input);
$cached = new CachingIterator($input);
print_r(iterator_to_array($cached));
print_r(iterator_to_array($cached));
use Gielfeldt\Iterators\ChecksumIterator;
$input = new \ArrayIterator([
['key1' => 'value1'],
['key2' => 'value2'],
['key3' => 'value3'],
]);
$iterator = new ChecksumIterator($input, 'md5');
foreach ($iterator as $key => $value) {
print "$key => $value\n";
}
var_dump($iterator->getChecksum());
var_dump((string) $iterator);
use Gielfeldt\Iterators\ChunkIterator;
use Gielfeldt\Iterators\AtomicTempFileObject;
// Split a file into multiple files of a 100 lines each.
$file = new \SplFileObject('inputfile');
foreach (new ChunkIterator($file, 100) as $i => $lines) {
AtomicTempFileObject::file_put_contents("outputfile.part.$i", implode("", iterator_to_array($lines)));
}
use Gielfeldt\Iterators\CombineIterator;
$keys = new \ArrayIterator(['key1', 'key2', 'key3', 'key1', 'key2', 'key3']);
$values = new \ArrayIterator(['value1', 'value2', 'value3', 'value4', 'value5', 'value6']);
$iterator = new CombineIterator($keys, $values);
foreach ($iterator as $key => $value) {
print "$key => $value\n";
}
print_r(iterator_to_array($iterator));
use Gielfeldt\Iterators\CountableIterator;
$some_noncountable_iterator = new \IteratorIterator(new \ArrayIterator([1, 2, 3]));
$iterator = new CountableIterator($some_noncountable_iterator);
var_dump(count($iterator));
use Gielfeldt\Iterators\FiniteIterator;
$input = new \ArrayIterator(range(1, 8));
$some = new FiniteIterator($input, function ($iterator) {
return $iterator->current() > 5;
});
print_r(iterator_to_array($some));
use Gielfeldt\Iterators\TraversableIterator;
$keys = range(1, 10);
shuffle($keys);
$values = range(1, 10);
shuffle($values);
$input = new \ArrayIterator(array_combine($keys, $values));
$iterator = new TraversableIterator($input);
foreach ($iterator as $k => $v) {
print $iterator->getIndex() . ": $k => $v\n";
}
use Gielfeldt\Iterators\KeysIterator;
$input = new \ArrayIterator([
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
]);
$iterator = new KeysIterator($input);
print_r(iterator_to_array($iterator));
use Gielfeldt\Iterators\MapIterator;
$input = new \ArrayIterator([
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
'key4' => 'value1',
'key5' => 'value2',
'key6' => 'value3',
]);
// Flip keys and values.
$iterator = new MapIterator($input, function ($iterator) {
return [$iterator->current(), $iterator->key()];
});
foreach ($iterator as $key => $value) {
print "$key => $value\n";
}
print_r(iterator_to_array($iterator));
use Gielfeldt\Iterators\RandomIterator;
$input = new \ArrayIterator(range(1, 10));
$random = new RandomIterator($input, 4);
var_dump(count($random));
print_r(iterator_to_array($random));
use Gielfeldt\Iterators\RepeatIterator;
$input = new \ArrayIterator([
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
]);
$iterator = new RepeatIterator($input, 3);
foreach ($iterator as $key => $value) {
print "$key => $value\n";
}
print_r(iterator_to_array($iterator));
use Gielfeldt\Iterators\ReplaceableIterator;
$iterator = new ReplaceableIterator(new \ArrayIterator(range(1, 4)));
print_r(iterator_to_array($iterator));
$iterator->setInnerIterator(new \ArrayIterator(range(5, 8)));
print_r(iterator_to_array($iterator));
use Gielfeldt\Iterators\ReservoirSamplingIterator;
$input = new \ArrayIterator(range(1, 10));
$input = new \NoRewindIterator($input);
$random = new ReservoirSamplingIterator($input, 4);
print_r(iterator_to_array($random));
use Gielfeldt\Iterators\ShuffleIterator;
$input = new \ArrayIterator(range(1, 10));
$shuffled = new ShuffleIterator($input);
print_r(iterator_to_array($shuffled));
print_r(iterator_to_array($shuffled));
use Gielfeldt\Iterators\SortIterator;
$input = new \ArrayIterator([6, 3, 2, 7, 1, 9]);
$iterator = new SortIterator($input);
print_r(iterator_to_array($iterator));
$input = new \ArrayIterator([6, 3, 2, 7, 1, 9]);
$iterator = new SortIterator($input, SortIterator::SORT_DESC);
print_r(iterator_to_array($iterator));
$input = new \ArrayIterator([6, 3, 2, 7, 1, 9]);
$iterator = new SortIterator($input, SortIterator::SORT_ASC, SortIterator::SORT_REINDEX);
print_r(iterator_to_array($iterator));
$input = new \ArrayIterator([6, 3, 2, 7, 1, 9]);
$iterator = new SortIterator($input, SortIterator::SORT_ASC, SortIterator::SORT_REINDEX, function ($a, $b) {
return -$a->current <=> -$b->current;
});
print_r(iterator_to_array($iterator));
use Gielfeldt\Iterators\StepIterator;
$input = new \ArrayIterator(range(1, 10));
$stepped = new StepIterator($input, 2);
print_r(iterator_to_array($stepped));
use Gielfeldt\Iterators\UniqueIterator;
$input = new \ArrayIterator([-4, -3, -2, -1, 0, 1, 2, 3, 5]);
// Unique elements by their square.
$iterator = new UniqueIterator($input, UniqueIterator::REINDEX, function ($iterator) {
return $iterator->current() * $iterator->current();
});
print_r(iterator_to_array($iterator));
use Gielfeldt\Iterators\ValuesIterator;
$input = new \ArrayIterator([
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
]);
$iterator = new ValuesIterator($input);
print_r(iterator_to_array($iterator));
use Gielfeldt\Iterators\CsvFileObject;
// Load csv file and dump it.
$file = new CsvFileObject('somefile.csv');
print_r(iterator_to_array($file));
// Same but csv comes via a string variable.
$csvdata = "Columm1,Column2\nValue1,Value2\nValue3,Value4";
$file = new CsvFileObject('data://application/octet,' . $csvdata);
print_r(iterator_to_array($file));
use Gielfeldt\Iterators\Iterator;
$input = new \ArrayIterator([1,2,3,4,5,6]);
var_dump(Iterator::sum($input));
var_dump(Iterator::product($input));
var_dump(Iterator::average($input));
var_dump(Iterator::min($input));
var_dump(Iterator::max($input));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.