Class ZipIterator
Creates an iterator that returns elements grouped in pairs
Example
$iterator = new ZipIterator([[1, 2], [3, 4]]); $iterator->toList(); // Returns [[1, 3], [2, 4]]
You can also chose a custom function to zip the elements together, such as doing a sum by index:
Example
$iterator = new ZipIterator([[1, 2], [3, 4]], function ($a, $b) { return $a + $b; }); $iterator->toList(); // Returns [4, 6]
- MultipleIterator implements Iterator, Traversable
- Cake\Collection\Iterator\ZipIterator implements Cake\Collection\CollectionInterface, Serializable uses Cake\Collection\CollectionTrait
Inherited Constants
-
MIT_KEYS_ASSOC
,MIT_KEYS_NUMERIC
,MIT_NEED_ALL
,MIT_NEED_ANY
Properties summary
-
$_callback
protectedcallable
The function to use for zipping items together -
$_iterators
protectedarray
Contains the original iterator objects that were attached
Method Summary
-
__construct() public
Creates the iterator to merge together the values by for all the passed iterators by their corresponding index.
-
current() public
Returns the value resulting out of zipping all the elements for all the iterators with the same positional index.
-
serialize() public
Returns a string representation of this object that can be used to reconstruct it
-
unserialize() public
Unserializes the passed string and rebuilds the ZipIterator instance
Method Detail
__construct() public ¶
__construct( array $sets , callable|null $callable null )
Creates the iterator to merge together the values by for all the passed iterators by their corresponding index.
Parameters
- array $sets
- The list of array or iterators to be zipped.
- callable|null $callable optional null
- The function to use for zipping the elements of each iterator.
Overrides
MultipleIterator::__construct()
current() public ¶
current( )
Returns the value resulting out of zipping all the elements for all the iterators with the same positional index.
Returns
Overrides
MultipleIterator::current()
Implementation of
Iterator::current()
serialize() public ¶
serialize( )
Returns a string representation of this object that can be used to reconstruct it
Returns
Implementation of
Serializable::serialize()
unserialize() public ¶
unserialize( string $iterators )
Unserializes the passed string and rebuilds the ZipIterator instance
Parameters
- string $iterators
- The serialized iterators
Implementation of
Serializable::unserialize()
Methods used from Cake\Collection\CollectionTrait
cartesianProduct() public ¶
cartesianProduct( callable $operation null , callable $filter null )
Parameters
- callable $operation optional null
- Operation
- callable $filter optional null
- Filter
Returns
Throws
optimizeUnwrap() protected ¶
optimizeUnwrap( )
Unwraps this iterator and returns the simplest traversable that can be used for getting the data out
Returns
Magic methods inherited from Cake\Collection\CollectionInterface
cartesianProduct()
|