Following types iterable
, ArrayObject
, Iterator
belong to collections.
Array extras like ::map
, ::reduce
do not make sense on a live collection,
one can rather convert the collection to an array (::toArray
) and then apply array extras.
Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is called.
{iterable|ArrayObject|Iterator} $collection
- source collection
chain({iterable|ArrayObject|Iterator} $collection)
<?php
$res = $res = Collections::chain(new \ArrayObject([1,2,3]))
->toArray()
->value();
echo $res; // "534"
Iterate over a list of elements, yielding each in turn to an $callable function
{iterable|ArrayObject|Iterator} $collection
- source collection{callable} $callable
- iteratee callback
each($collection, callable $callable)
<?php
$sum = 0;
$obj = new \ArrayObject([1,2,3]);
Collections::each($obj->getIterator(), function ($i) use (&$sum){
$sum += $i;
});
Convert collectionb to an array
{iterable|ArrayObject|Iterator} $collection
- source collection
toArray($collection)
<?php
$sum = 0;
$obj = new \ArrayObject([1,2,3]);
$res = Collections::toArray();