https://github.com/bpolaszek/flatten-iterator
Flattens Traversable or arrays into one iterator.
https://github.com/bpolaszek/flatten-iterator
Last synced: 3 months ago
JSON representation
Flattens Traversable or arrays into one iterator.
- Host: GitHub
- URL: https://github.com/bpolaszek/flatten-iterator
- Owner: bpolaszek
- License: mit
- Created: 2017-07-17T10:13:22.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-11-11T06:02:29.000Z (about 3 years ago)
- Last Synced: 2025-02-01T05:12:34.171Z (11 months ago)
- Language: PHP
- Size: 5.86 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/bentools/flatten-iterator)
[](https://packagist.org/packages/bentools/flatten-iterator)
[](https://github.com/bpolaszek/flatten-iterator/actions/workflows/ci-workflow.yml)
[](https://codecov.io/gh/bpolaszek/flatten-iterator)
[](https://scrutinizer-ci.com/g/bpolaszek/flatten-iterator)
[](https://packagist.org/packages/bentools/flatten-iterator)
# FlattenIterator
Flattens arrays and / or traversables. Accepts any `iterable` composed of `iterables`.
PHP 7.1+
Usage
-------
```php
use BenTools\FlattenIterator\FlattenIterator;
require_once __DIR__ . '/vendor/autoload.php';
$cities = [
[
'london' => 'London',
'paris' => 'Paris',
],
new \ArrayIterator([
'berlin' => 'Berlin',
'bruxelles' => 'Bruxelles',
]),
(function () {
yield 'budapest' => 'Budapest';
yield 'prague' => 'Prague';
})(),
];
foreach (new FlattenIterator($cities) as $city) {
var_dump($city);
}
```
Output:
```
string(6) "London"
string(5) "Paris"
string(6) "Berlin"
string(9) "Bruxelles"
string(8) "Budapest"
string(6) "Prague"
```
Array output and fluent interface
---------------------------------
You can use the built-in function to generate your flattened data, and export them as an array:
```php
use function BenTools\FlattenIterator\flatten;
print_r(flatten($cities)->asArray());
```
Output:
```php
array(6) {
[0]=>
string(6) "London"
[1]=>
string(5) "Paris"
[2]=>
string(6) "Berlin"
[3]=>
string(9) "Bruxelles"
[4]=>
string(8) "Budapest"
[5]=>
string(6) "Prague"
}
```
Preserve Keys
-------------
Set `$preserveKeys` to `true` to preserve keys in your flattened data:
```php
var_dump(flatten($cities, $preserveKeys = true)->asArray());
```
Output:
```
array(6) {
["london"]=>
string(6) "London"
["paris"]=>
string(5) "Paris"
["berlin"]=>
string(6) "Berlin"
["bruxelles"]=>
string(9) "Bruxelles"
["budapest"]=>
string(8) "Budapest"
["prague"]=>
string(6) "Prague"
}
```
Installation
------------
```
composer require bentools/flatten-iterator
```
Unit tests
----------
```
./vendor/bin/phpunit
```
See also
--------
[bentools/cartesian-product](https://github.com/bpolaszek/cartesian-product)
[bentools/string-combinations](https://github.com/bpolaszek/string-combinations)
[bentools/iterable-functions](https://github.com/bpolaszek/php-iterable-functions)