PHP code example of horat1us / map-iterator

1. Go to this page and download the library: Download horat1us/map-iterator 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/ */

    

horat1us / map-iterator example snippets




use Horat1us\Util\MapIterator;

class IntegerPlusOneInterator extends MapIterator
{
    public function map($item)
    {
        if (!is_int($item)) {
            throw new \InvalidArgumentException("Unable map not-integer item.");
        }
        return $item + 1;
    }
}

$numbers = [1,2,3];
$iterator = new IntegerPlusOneInterator(new \ArrayIterator($numbers));
print_r(iterator_to_array($iterator)); // Array ( [0] => 2, [1] => 3, [2] => 4 )




use Horat1us\Util\CallbackMapIterator;

$numbers = [1,2,3];
$powCallback = static fn(int $number) => pow($number, $number);
$iterator = new CallbackMapIterator(new \ArrayIterator($numbers), $powCallback);

print_r(iterator_to_array($iterator)); // Array ( [0] => 1, [1] => 4, [2] => 27 )