{"id":16463716,"url":"https://github.com/bpolaszek/php-iterable-functions","last_synced_at":"2025-08-24T19:07:25.920Z","repository":{"id":37773885,"uuid":"84562835","full_name":"bpolaszek/php-iterable-functions","owner":"bpolaszek","description":"Provides functions for iterable variables: iterable_map(, iterable_filter(), iterable_to_array(), ...","archived":false,"fork":false,"pushed_at":"2024-06-04T14:07:04.000Z","size":106,"stargazers_count":23,"open_issues_count":1,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-11T00:45:27.063Z","etag":null,"topics":["array","iterable","iterator","iteratoraggregate","php","traversable"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bpolaszek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-10T13:31:26.000Z","updated_at":"2024-06-04T13:23:40.000Z","dependencies_parsed_at":"2024-03-27T14:27:58.991Z","dependency_job_id":"2e7c5c21-d764-4b42-a7dd-2d4e81e4e04d","html_url":"https://github.com/bpolaszek/php-iterable-functions","commit_stats":{"total_commits":55,"total_committers":6,"mean_commits":9.166666666666666,"dds":0.4727272727272728,"last_synced_commit":"6a094a258e44edbbf7431ab0a57e27719dd576e5"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bpolaszek%2Fphp-iterable-functions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bpolaszek%2Fphp-iterable-functions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bpolaszek%2Fphp-iterable-functions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bpolaszek%2Fphp-iterable-functions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bpolaszek","download_url":"https://codeload.github.com/bpolaszek/php-iterable-functions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248322604,"owners_count":21084336,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","iterable","iterator","iteratoraggregate","php","traversable"],"created_at":"2024-10-11T11:15:17.566Z","updated_at":"2025-04-11T00:45:36.651Z","avatar_url":"https://github.com/bpolaszek.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Latest Stable Version](https://poser.pugx.org/bentools/iterable-functions/v/stable)](https://packagist.org/packages/bentools/iterable-functions)\n[![GitHub Actions][GA master image]][GA master]\n[![Code Coverage][Coverage image]][CodeCov Master]\n[![Shepherd Type][Shepherd Image]][Shepherd Link]\n[![Total Downloads](https://poser.pugx.org/bentools/iterable-functions/downloads)](https://packagist.org/packages/bentools/iterable-functions)\n\nIterable functions\n==================\n\nThis package provides functions to work with [iterables](https://wiki.php.net/rfc/iterable), as you usually do with arrays:\n\n- [iterable_to_array()](#iterable_to_array)\n- [iterable_to_traversable()](#iterable_to_traversable)\n- [iterable_map()](#iterable_map)\n- [iterable_merge()](#iterable_merge)\n- [iterable_reduce()](#iterable_reduce)\n- [iterable_filter()](#iterable_filter)\n- [iterable_values()](#iterable_values)\n- [iterable_chunk()](#iterable_chunk)\n\niterable_to_array()\n-------------------\n\nPHP offers an `iterator_to_array()` function to export any iterator into an array.\n\n**But when you want to transform an `iterable` to an array, the `iterable` itself can already be an array.**\n\nWhen using `iterator_to_array()` with an iterable, that happens to be an array, PHP will throw a `TypeError`.\n\nIf you need an iterable-agnostic function, try our `iterable_to_array()`:\n\n```php\nuse function BenTools\\IterableFunctions\\iterable_to_array;\n\nvar_dump(iterable_to_array(new \\ArrayIterator(['foo', 'bar']))); // ['foo', 'bar']\nvar_dump(iterable_to_array(['foo', 'bar'])); // ['foo', 'bar']\n```\n\niterable_to_traversable()\n-------------------------\nUseful when you have a `Traversable` type-hint, and you don't know wether or not your argument will be an array or an iterator.\n\nIf your variable is already an instance of `Traversable` (i.e. an `Iterator`, an `IteratorAggregate` or a `Generator`), the function simply returns it directly.\n\nIf your variable is an array, the function converts it to an `ArrayIterator`.\n\nUsage:\n\n```php\nuse function BenTools\\IterableFunctions\\iterable_to_traversable;\n\nvar_dump(iterable_to_traversable(['foo', 'bar'])); // \\ArrayIterator(['foo', 'bar'])\nvar_dump(iterable_to_traversable(new \\ArrayIterator(['foo', 'bar']))); // \\ArrayIterator(['foo', 'bar'])\n```\n\niterable_map()\n--------------\n\nWorks like an `array_map` with an `array` or a `Traversable`.\n\n```php\nuse function BenTools\\IterableFunctions\\iterable_map;\n\n$generator = function () {\n    yield 'foo';\n    yield 'bar';\n};\n\nforeach (iterable_map($generator(), 'strtoupper') as $item) {\n    var_dump($item); // FOO, BAR\n}\n```\n\niterable_merge()\n--------------\n\nWorks like an `array_merge` with an `array` or a `Traversable`.\n\n```php\nuse function BenTools\\IterableFunctions\\iterable_merge;\n\n$generator1 = function () {\n    yield 'foo';\n};\n\n$generator2 = function () {\n    yield 'bar';\n};\n\nforeach (iterable_merge($generator1(), $generator2()) as $item) {\n    var_dump($item); // foo, bar\n}\n```\n\niterable_reduce()\n--------------\n\nWorks like an `reduce` with an `iterable`.\n\n```php\nuse function BenTools\\IterableFunctions\\iterable_reduce;\n\n$generator = function () {\n    yield 1;\n    yield 2;\n};\n\n$reduce = static function ($carry, $item) {\n    return $carry + $item;\n};\n\nvar_dump(\n    iterable_reduce($generator(), $reduce, 0))\n); // 3\n```\n\niterable_filter()\n--------------\n\nWorks like an `array_filter` with an `array` or a `Traversable`.\n\n```php\nuse function BenTools\\IterableFunctions\\iterable_filter;\n\n$generator = function () {\n    yield 0;\n    yield 1;\n};\n\nforeach (iterable_filter($generator()) as $item) {\n    var_dump($item); // 1\n}\n```\n\nOf course you can define your own filter:\n\n```php\nuse function BenTools\\IterableFunctions\\iterable_filter;\n\n$generator = function () {\n    yield 'foo';\n    yield 'bar';\n};\n\n$filter = function ($value) {\n    return 'foo' !== $value;\n};\n\n\nforeach (iterable_filter($generator(), $filter) as $item) {\n    var_dump($item); // bar\n}\n```\n\niterable_values()\n--------------\n\nWorks like an `array_values` with an `array` or a `Traversable`.\n\n```php\nuse function BenTools\\IterableFunctions\\iterable_values;\n\n$generator = function () {\n    yield 'a' =\u003e 'a';\n    yield 'b' =\u003e 'b';\n};\n\nforeach (iterable_values($generator()) as $key =\u003e $value) {\n    var_dump($key); // 0, 1\n    var_dump($value); // a, b\n}\n```\n\niterable_chunk()\n--------------\n\nHere's an `array_chunk`-like function that also works with a `Traversable`.\n\n```php\nuse function BenTools\\IterableFunctions\\iterable_chunk;\n\n$fruits = [\n    'banana',\n    'apple',\n    'strawberry',\n    'raspberry',\n    'pineapple',\n]\n$fruits = (fn () =\u003e yield from $fruits)()\niterable_chunk($fruits, 2);\n\n/*\n  [\n    ['banana', 'apple'],\n    ['strawberry', 'raspberry'],\n    ['pineapple'],\n  ]\n */\n```\n\nIterable fluent interface\n=========================\n\nThe `iterable` function allows you to wrap an iterable and apply some common operations.\n\nWith an array input:\n\n```php\nuse function BenTools\\IterableFunctions\\iterable;\n$data = [\n    'banana',\n    'pineapple',\n    'rock',\n];\n\n$iterable = iterable($data)-\u003efilter(fn($eatable) =\u003e 'rock' !== $eatable)-\u003emap('strtoupper'); // Traversable of ['banana', 'pineapple']\n```\n\nWith a traversable input:\n\n```php\nuse function BenTools\\IterableFunctions\\iterable;\n$data = [\n    'banana',\n    'pineapple',\n    'rock',\n];\n\n$data = fn() =\u003e yield from $data;\n\n$iterable = iterable($data())-\u003efilter(fn($eatable) =\u003e 'rock' !== $eatable)-\u003emap('strtoupper'); // Traversable of ['banana', 'pineapple']\n```\n\nArray output:\n\n```php\n$iterable-\u003easArray(); // array ['banana', 'pineapple']\n```\n\nInstallation\n============\n\n```\ncomposer require bentools/iterable-functions:^2.0\n```\n\nFor PHP5+ compatibility, check out the [1.x branch](https://github.com/bpolaszek/php-iterable-functions/tree/1.x).\n\n\nUnit tests\n==========\n\n```\nphp vendor/bin/pest\n```\n\n[GA master]: https://github.com/bpolaszek/php-iterable-functions/actions?query=workflow%3A%22Continuous+Integration%22+branch%3A2.0.x-dev\n\n[GA master image]: https://github.com/bpolaszek/php-iterable-functions/workflows/Continuous%20Integration/badge.svg\n\n[CodeCov Master]: https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev\n\n[Coverage image]: https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev/graph/badge.svg\n\n[Shepherd Image]: https://shepherd.dev/github/bpolaszek/php-iterable-functions/coverage.svg\n\n[Shepherd Link]: https://shepherd.dev/github/bpolaszek/php-iterable-functions\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbpolaszek%2Fphp-iterable-functions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbpolaszek%2Fphp-iterable-functions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbpolaszek%2Fphp-iterable-functions/lists"}