Download the PHP package bentools/iterable-functions without Composer
On this page you can find all versions of the php package bentools/iterable-functions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bentools/iterable-functions
More information about bentools/iterable-functions
Files in bentools/iterable-functions
Package iterable-functions
Short Description Provides functions for iterable variables: is_iterable(), iterable_to_array()
License MIT
Informations about the package iterable-functions
Iterable functions
This package provides functions to work with iterables, as you usually do with arrays:
- iterable_to_array()
- iterable_to_traversable()
- iterable_map()
- iterable_merge()
- iterable_reduce()
- iterable_filter()
- iterable_values()
- iterable_chunk()
iterable_to_array()
PHP offers an iterator_to_array()
function to export any iterator into an array.
But when you want to transform an iterable
to an array, the iterable
itself can already be an array.
When using iterator_to_array()
with an iterable, that happens to be an array, PHP will throw a TypeError
.
If you need an iterable-agnostic function, try our iterable_to_array()
:
iterable_to_traversable()
Useful when you have a Traversable
type-hint, and you don't know wether or not your argument will be an array or an iterator.
If your variable is already an instance of Traversable
(i.e. an Iterator
, an IteratorAggregate
or a Generator
), the function simply returns it directly.
If your variable is an array, the function converts it to an ArrayIterator
.
Usage:
iterable_map()
Works like an array_map
with an array
or a Traversable
.
iterable_merge()
Works like an array_merge
with an array
or a Traversable
.
iterable_reduce()
Works like an reduce
with an iterable
.
iterable_filter()
Works like an array_filter
with an array
or a Traversable
.
Of course you can define your own filter:
iterable_values()
Works like an array_values
with an array
or a Traversable
.
iterable_chunk()
Here's an array_chunk
-like function that also works with a Traversable
.
Iterable fluent interface
The iterable
function allows you to wrap an iterable and apply some common operations.
With an array input:
With a traversable input:
Array output:
Installation
For PHP5+ compatibility, check out the 1.x branch.