https://github.com/enzyme/loopy
A loop library for PHP
https://github.com/enzyme/loopy
Last synced: 8 months ago
JSON representation
A loop library for PHP
- Host: GitHub
- URL: https://github.com/enzyme/loopy
- Owner: enzyme
- License: mit
- Created: 2015-12-07T22:33:03.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-04-03T07:55:12.000Z (about 10 years ago)
- Last Synced: 2025-05-30T03:39:53.734Z (about 1 year ago)
- Language: PHP
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://travis-ci.org/enzyme/loopy)
[](https://coveralls.io/github/enzyme/loopy?branch=master)
[](https://scrutinizer-ci.com/g/enzyme/loopy/?branch=master)
A loop library for PHP.
# Installation
Run the following command inside of your project directory.
```bash
composer require enzyme/loopy
```
# Usage
#### Simple
A simple loop over an array.
```php
use Enzyme\Loopy\Each;
$array = [1, 2, 3];
Each::shallow()->begin($array, function($bag) {
echo $bag->value() . ', ';
});
```
The above will output `1, 2, 3, `
#### Deep
A deep loop over a multidimensional array.
```php
use Enzyme\Loopy\Each;
$array = [1, 2, 3, 4 => [4, 5, 6]];
Each::deep()->begin($array, function($bag) {
echo $bag->value() . ', ';
});
```
The above will output `1, 2, 3, 4, 5, 6, `
#### Filtered
A loop over a filtered array.
```php
use Enzyme\Loopy\Each;
use Enzyme\Loopy\Filters\SkipNulls;
$array = [1, 2, null, 4, 5];
Each::deep(new SkipNulls)->begin($array, function($bag) {
echo $bag->value() . ', ';
});
```
The above will output `1, 2, 4, 5, `
# Bags
The `$bag` object passed to the callback function generally contains the follow information.
Key | Value | Description
----|-------|------------
key() | Key | The enumerable objects key for the given pass.
value() | Value | The enumerable objects value for the given pass.
cycle() | Current cycle (0 based) | How many times around the entire object the looper has gone.
depth() | Current depth (0 based) | The current depth of the pass, for multidimensional arrays.
index() | Current index (0 based) | The current index, in this case index is really a count of the number of items encountered so far.
# Filters
Filter | Description
-------|------------
SkipNulls | Will skip over any values which are null.
Regex | Will test value against the supplied regular expression.
Equal | Will test value for equality against the given rule. (===)
Less | Will test that value is less than the give rule, or less than or equal if parameter 2 in the constructor is set to true.
Greater | Will test that value is greater than the give rule, or greater than or equal if parameter 2 in the constructor is set to true.